用UIControl封装Button
效果
说明
UIControl在处理超出触摸范围的触摸事件时有bug
源码
基础类
// // BaseControl.h // BaseControl // // Created by YouXianMing on 15/8/26. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interface BaseControl : UIControl /** * ==== 由子类重写 ==== * * 开始触发事件 */ - (void)beginTouch; /** * ==== 由子类重写 ==== * * 结束触发事件 * * @param outRange 是否超出操作范围 */ - (void)endTouchOutOfRange:(BOOL)outRange; @end
// // BaseControl.m // BaseControl // // Created by YouXianMing on 15/8/26. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "BaseControl.h" @interface BaseControl () @property (nonatomic) CGPoint endPoint; @end @implementation BaseControl #pragma mark - - (void)beginTouch { } - (void)endTouchOutOfRange:(BOOL)outRange { } #pragma mark - UIControl事件 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [self beginTouch]; return YES; } - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { return YES; } - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { self.endPoint = [touch locationInView:self]; } - (void)cancelTrackingWithEvent:(UIEvent *)event { [super cancelTrackingWithEvent:event]; } - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { CGPoint point = self.endPoint; if (point.x >= 0 && point.x <= self.bounds.size.width && point.y >= 0 && point.y <= self.bounds.size.height) { [self endTouchOutOfRange:NO]; [super sendAction:action to:target forEvent:event]; } else { [self endTouchOutOfRange:YES]; [super sendAction:action to:target forEvent:event]; } } @end
继承子类
// // ColorButton.h // BaseControl // // Created by YouXianMing on 15/8/26. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "BaseControl.h" @interface ColorButton : BaseControl @end
// // ColorButton.m // BaseControl // // Created by YouXianMing on 15/8/26. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "ColorButton.h" @implementation ColorButton - (void)beginTouch { [UIView animateWithDuration:0.4f delay:0.f usingSpringWithDamping:1.f initialSpringVelocity:0.f options:0 animations:^{ self.backgroundColor = [UIColor redColor]; } completion:^(BOOL finished) { }]; } - (void)endTouchOutOfRange:(BOOL)outRange { [UIView animateWithDuration:0.4f delay:0.f usingSpringWithDamping:1.f initialSpringVelocity:0.f options:0 animations:^{ self.backgroundColor = [UIColor whiteColor]; } completion:^(BOOL finished) { }]; } @end
使用
// // ViewController.m // BaseControl // // Created by YouXianMing on 15/8/26. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "ColorButton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ColorButton *control = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; control.center = self.view.center; control.layer.borderWidth = 1.f; [self.view addSubview:control]; [control addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside]; } - (void)buttonEvent:(id)sender { } @end
细节