iOS开发学习之路【UI界面】——手势处理

本文详细介绍了iOS中常见的手势识别处理方式,包括点击、捏合、旋转、滑动、拖动及长按等,并提供了具体的实现代码示例。

手势处理

点击

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 1.创建一个触屏事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    // 2.为视图添加手势
    [self.view addGestureRecognizer:tap];
    
}
    // 3.处理手势的方法
-(void)tap:(UITapGestureRecognizer*)param{
    CGPoint point = [param locationInView:self.view];
    if(CGRectContainsPoint(self.myLabel.frame, point)){
        self.myLabel.backgroundColor = [UIColor yellowColor];
    }else{
        self.myLabel.backgroundColor = [UIColor redColor];
    }
}

捏合、旋转

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 捏 (放大、缩小)
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [self.view addGestureRecognizer:pinch];
    
    // 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotate:)];
    [self.view addGestureRecognizer:rotation];
    
}

-(void)pinch:(UIPinchGestureRecognizer*)param{
    CGFloat scale = param.scale;
    self.myImage.transform = CGAffineTransformMakeScale(scale, scale);
}
-(void)rotate:(UIRotationGestureRecognizer*)param{
    CGFloat r = param.rotation;
    self.myImage.transform = CGAffineTransformMakeRotation(r);
}

滑动

 	UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];
    
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

拖动

	UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];

长按

	UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    [self.view addGestureRecognizer:longPress];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值