赞
踩
这几天做到录音,要求是 按住录音,松开停止录音完成, 手拖到按钮外时取消录音
先看看UIControlEvent的解释
- UIControlEventTouchDown
- 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
- UIControlEventTouchDownRepeat
- 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
- UIControlEventTouchDragInside
- 当一次触摸在控件窗口内拖动时。
- UIControlEventTouchDragOutside
- 当一次触摸在控件窗口之外拖动时。
- UIControlEventTouchDragEnter
- 当一次触摸从控件窗口之外拖动到内部时。
- UIControlEventTouchDragExit
- 当一次触摸从控件窗口内部拖动到外部时。
-
- UIControlEventTouchUpInside
- 所有在控件之内触摸抬起事件。
- UIControlEventTouchUpOutside
- 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
- UIControlEventTouchCancel
- 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
- UIControlEventTouchChanged
- 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
- UIControlEventEditingDidBegin
- 当文本控件中开始编辑时发送通知。
- UIControlEventEditingChanged
- 当文本控件中的文本被改变时发送通知。
- UIControlEventEditingDidEnd
- 当文本控件中编辑结束时发送通知。
- UIControlEventEditingDidOnExit
- 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
- UIControlEventAlltouchEvents
- 通知所有触摸事件。
- UIControlEventAllEditingEvents
- 通知所有关于文本编辑的事件。
- UIControlEventAllEvents
- 通知所有事件。
- #import "ViewController.h"
-
- @interface ViewController ()
- @property (nonatomic, strong) UIButton *recordButton;
- @property (nonatomic, strong) UIActivityIndicatorView *indicator;
- @end
-
- @implementation ViewController
-
- - (UIActivityIndicatorView *)indicator
- {
- if (!_indicator) {
- _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- _indicator.center = _recordButton.center;
- [self.view addSubview:_indicator];
- }
- return _indicator;
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
-
- _recordButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
- _recordButton.backgroundColor = [UIColor redColor];
- // 开始
- [_recordButton addTarget:self action:@selector(recordStart:) forControlEvents:UIControlEventTouchDown];
- // 取消
- [_recordButton addTarget:self action:@selector(recordCancel:) forControlEvents: UIControlEventTouchDragExit | UIControlEventTouchUpOutside];
- //完成
- [_recordButton addTarget:self action:@selector(recordFinish:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_recordButton];
- _recordButton.layer.cornerRadius = 50;
- }
-
- - (void)recordStart:(UIButton *)button
- {
- [self.indicator startAnimating];
- [button setTitle:@"录制中" forState:UIControlStateNormal];
- NSLog(@"UIControlEventTouchDown---recordStart");
- }
-
- - (void)recordCancel:(UIButton *)button
- {
- if (self.indicator.isAnimating) {
- [self.indicator stopAnimating];
- }
- [self.recordButton setTitle:@"重新录制" forState:UIControlStateNormal];
- NSLog(@"UIControlEventTouchDragExit 和 UIControlEventTouchUpOutside---recordCancel");
- }
-
- - (void)recordFinish:(UIButton *)button
- {
- NSLog(@"UIControlEventTouchUpInside---recordFinish");
- [button setTitle:@"完成" forState:UIControlStateNormal];
- [self.indicator stopAnimating];
- }
这样会有个问题就是手已经滑到按钮边界外了 还没触发取消的 感觉有点奇怪
参考:http://www.cnblogs.com/peterpan507/p/3632353.html?utm_source=tuicool
- - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
- {
- // 设置新的边界
- CGFloat boundsExtension = 25.0f;
- CGRect outerBounds = CGRectInset(self.bounds, -1 * boundsExtension, -1 * boundsExtension);
- // 判断触摸位置
- BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:self]);
- if(touchOutside)
- {
- // 判断是UIControlEventTouchDragExit/UIControlEventTouchDragOutside
- BOOL previousTouchInside = CGRectContainsPoint(outerBounds, [touch previousLocationInView:self]);
- if(previousTouchInside)
- {
- [self sendActionsForControlEvents:UIControlEventTouchDragExit];
- }
- else
- {
- [self sendActionsForControlEvents:UIControlEventTouchDragOutside];
- }
- }
- // 如果不是想要修改的control event,返回原操作
- return [super continueTrackingWithTouch:touch withEvent:event];
- }
把触发区域缩小了 但是还是有个问题就是touchupinside的识别区域 也是超出按钮的 也就是说调完touupoutside以后(如果你距离按钮区域不远的话)还会再调用touchupinside的方法
增加一个标志来代表已经取消了
- - (void)recordStart:(UIButton *)button
- {
- _isCancel = NO;
- [self.indicator startAnimating];
- [self.recordButton setTitle:@"录制中" forState:UIControlStateNormal];
- }
-
- - (void)recordCancel:(UIButton *)button
- {
- _isCancel = YES;
- if (self.indicator.isAnimating) {
- [self.indicator stopAnimating];
- }
- [self.recordButton setTitle:@"重新录制" forState:UIControlStateNormal];
- }
-
- - (void)recordFinish:(UIButton *)button
- {
- if (!_isCancel) {
- [self.recordButton setTitle:@"完成" forState:UIControlStateNormal];
- [self.indicator stopAnimating];
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。