iOS开发-处理UIControl触摸事件TrackingWithEvent

IOS BUG记录 之 处理UIControl的点击事件。

UIControl的触摸事件的方法是beginTrackingWithTouch:withEvent:,continueTrackingWithTouch:withEvent:,endTrackingWithTouch:withEvent:,cancelTrackingWithEvent:

##下面简单的介绍一下

  • beginTrackingWithTouch:withEvent:是控件被拖动,让其进入高亮状态;
  • endTrackingWithTouch:withEvent:结束触摸,取消高亮,恢复状态。
  • continueTrackingWithTouch:withEvent:手指移动过程。
  • cancelTrackingWithEvent: 取消触摸,取消高亮,恢复状态。

今天遇到一个问题BUG,当点击时直接移开手指时要求被点击的控件变小后恢复状态;还需要长时间触摸在该控件上,手指不移开的时候,控件一直保持在变小的状态,一段时间移开手指时控件恢复正常的状态,以便响应用户的操作。

使用的控件继承UIControl

##第一种情况:当点击时直接移开手指时要求被点击的控件变小后恢复状态;

点击触发事件问题,UIControlEventTouchUpInside

复制代码
        [view addTarget:self action:@selector(viewClicked:) forControlEvents:UIControlEventTouchUpInside];

点击事件的方法

复制代码
        - (void) viewClicked:(id)sender {
    DFBaseView *view = (DFBaseView *)sender;
    view.userInteractionEnabled = NO;
    
    __weak typeof(view) weakView = view;
    
    __weak typeof(self) weakSelf = self;
    [view didSelectedAnimation:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        __weak typeof(weakView) strongView = weakView;

        //todo something
        strongView.userInteractionEnabled = YES;
    }];
}

点击的时候的动画处理,先变小后恢复

复制代码
        - (void)didSelectedAnimation:(void (^) (void))block {
    [UIView animateWithDuration:0.15 animations:^{
        self.layer.transform = CATransform3DMakeScale(kTransformScale, kTransformScale, 1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 animations:^{
            self.layer.transform = CATransform3DMakeScale(1.0, 1.0, 1);
        } completion:^(BOOL finished) {
            self.animationHighlighted = NO;
            block();
        }];
    }];
}

从上面的代码,我们可以看出,是UIControl的点击事件,动画结束后回调block,之后//todo something 如push跳转等操作。

##第二种情况:长时间触摸在该控件上,手指不移开的时候,控件一直保持在变小的状态,一段时间移开手指时控件恢复正常的状态。

这个时候上面的代码就满足不了了,需要实现如下的几个方法

  • (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
  • (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event; // touch is sometimes nil if cancelTracking calls through to this.
  • (void)cancelTrackingWithEvent:(nullable UIEvent *)event; // event may be nil if cancelled for non-event reasons, e.g. removed from window

具体实现

复制代码
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [UIView animateWithDuration:0.15 animations:^{
        self.layer.transform = CATransform3DMakeScale(kTransformScale, kTransformScale, 1);
    } completion:^(BOOL finished) {
    }];
    return [super beginTrackingWithTouch:touch withEvent:event];
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    return [super continueTrackingWithTouch:touch withEvent:event];
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [super endTrackingWithTouch:touch withEvent:event];
    [UIView animateWithDuration:0.15 animations:^{
        self.layer.transform = CATransform3DMakeScale(1.0, 1.0, 1);
    } completion:^(BOOL finished) {
    }];
}

- (void)cancelTrackingWithEvent:(UIEvent *)event {
    [super cancelTrackingWithEvent:event];
    [UIView animateWithDuration:0.15 animations:^{
        self.layer.transform = CATransform3DMakeScale(1.0, 1.0, 1);
    } completion:^(BOOL finished) {
    }];
}

学习记录,每天不停进步。

相关推荐
二流小码农36 分钟前
鸿蒙开发:个人开发者如何使用华为账号登录
android·ios·harmonyos
转战英雄枫42 分钟前
Mac上打开安卓虚拟机BlueStacks Air闪退问题解决
macos·bluestacks
liliangcsdn4 小时前
mac m1安装homebrew和iterm2示例
macos
wvy4 小时前
Xcode 26还没有适配SceneDelegate的app建议尽早适配
ios
墨&白.4 小时前
如何卸载/更新Mac上的R版本
开发语言·macos·r语言
游戏开发爱好者85 小时前
苹果 App 上架流程,结合 Xcode、CI 等常见工具
macos·ios·ci/cd·小程序·uni-app·iphone·xcode
前端老白5 小时前
webview在微信小程序中,安卓加载失败,IOS正常加载
android·ios·微信小程序·webview
2501_915106325 小时前
用 HBuilder 上架 iOS 应用时如何管理Bundle ID、证书与描述文件
android·ios·小程序·https·uni-app·iphone·webview
2501_915909065 小时前
资源文件混淆在 iOS 应用安全中的实际价值
android·安全·ios·小程序·uni-app·iphone·webview
2501_915918415 小时前
iOS App 性能测试中常被忽略的运行期问题
android·ios·小程序·https·uni-app·iphone·webview