MST

星途 面试题库

面试题:Objective-C在iOS触摸事件处理中,如何实现复杂手势识别与动画联动?

假设需要在Objective-C编写的iOS应用中实现一个复杂手势(如特定轨迹的滑动手势),识别该手势后触发一系列动画效果(如视图的旋转、缩放等),请描述实现思路并给出大致的代码架构。
44.3万 热度难度
编程语言Objective-C

知识考点

AI 面试

面试题答案

一键面试

实现思路

  1. 手势识别
    • 创建一个自定义的手势识别器,继承自UIGestureRecognizer类。在自定义手势识别器中,重写touchesBegan:withEvent:touchesMoved:withEvent:touchesEnded:withEvent:等方法,通过这些方法来跟踪手指的移动轨迹,判断是否符合特定轨迹的滑动手势。
    • 例如,在touchesMoved:withEvent:方法中,可以记录每次手指移动的位置,将这些位置点保存到一个数组中,然后根据数组中的点判断是否符合设定的轨迹。
  2. 动画效果
    • 一旦手势识别成功,利用UIView的动画方法来实现视图的旋转、缩放等效果。可以使用UIView的类方法animateWithDuration:animations:来创建动画块,在动画块中设置视图的transform属性来实现旋转和缩放。
    • 比如,要实现旋转,可以设置CGAffineTransformMakeRotation,要实现缩放,可以设置CGAffineTransformMakeScale,然后将这些变换组合应用到视图的transform属性上。

大致代码架构

  1. 自定义手势识别器类
#import <UIKit/UIKit.h>

@interface CustomGestureRecognizer : UIGestureRecognizer

@end

#import "CustomGestureRecognizer.h"

@implementation CustomGestureRecognizer {
    NSMutableArray<NSValue *> *touchPoints;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        touchPoints = [NSMutableArray array];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    for (UITouch *touch in touches) {
        CGPoint point = [touch locationInView:self.view];
        [touchPoints addObject:[NSValue valueWithCGPoint:point]];
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    for (UITouch *touch in touches) {
        CGPoint point = [touch locationInView:self.view];
        [touchPoints addObject:[NSValue valueWithCGPoint:point]];
        // 这里添加判断轨迹的逻辑,比如判断点的分布是否符合特定轨迹
        // 如果符合,设置手势状态为UIGestureRecognizerStateRecognized
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    // 再次确认轨迹是否符合要求,最终确定手势状态
    if (/* 符合特定轨迹 */) {
        self.state = UIGestureRecognizerStateRecognized;
    } else {
        self.state = UIGestureRecognizerStateFailed;
    }
    [touchPoints removeAllObjects];
}
@end
  1. 视图控制器中使用手势识别器并实现动画
#import "ViewController.h"
#import "CustomGestureRecognizer.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *targetView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.targetView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    self.targetView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.targetView];
    
    CustomGestureRecognizer *customGesture = [[CustomGestureRecognizer alloc] init];
    [customGesture addTarget:self action:@selector(handleCustomGesture:)];
    [self.view addGestureRecognizer:customGesture];
}

- (void)handleCustomGesture:(CustomGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        [UIView animateWithDuration:0.5 animations:^{
            CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_4);
            CGAffineTransform scale = CGAffineTransformMakeScale(1.2, 1.2);
            self.targetView.transform = CGAffineTransformConcat(rotation, scale);
        }];
    }
}
@end