事件和手势

code

让视图跟着手指移动

之前先设置一个CGPoint得属性

@property (nonatomic, assign) CGPoint myPoint;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    // 获取触摸点的坐标
    // 1.先获取touch对象
    UITouch *touch = [touches anyObject];
    self.myPoint = [touch locationInView: self];

}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint newPoint = [touch locationInView: self];
    // 计算起始和过程的变化
    CGFloat x = newPoint.x - self.myPoint.x;
    CGFloat y = newPoint.y - self.myPoint.y;
    self.center = CGPointMake(self.center.x + x , self.center.y + y);
}

事件

触摸

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@"触摸开始");
    // 在这里让textField失去第一响应者==》触摸父视图,回收键盘

}
- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    NSLog(@"触摸结束");
}
- (void) touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    NSLog(@"触摸被取消");
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    NSLog(@"触摸移动");
}

摇一摇

摇一摇开始

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionBegan:motion withEvent:event];

}

摇一摇结束

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionEnded:motion withEvent:event];

}

手势

使图片添加手势等操作,需要先将对象的用户交互打开

self.viewNeedToAddGesture.userInteractionEnabled = YES;

轻点

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(tapAction:)];
    [self.viewNeedToTap addGestureRecognizer: tap];
    [tap release];

- (void) tapAction: (UITapGestureRecognizer *) tap {
    NSLog(@"图片完成点击");
} 

property

  • numberOfTapsRequired 需要tap的次数
  • numberOfTouchesRequired 需要的手指数目

长按

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.viewNeedToLongPress addGestureRecognizer: longPress];
    [longPress release];

- (void) longPressAction: (UITapGestureRecognizer *)longPress {
    // 通过手势的状态避免重复的触发手势方法
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"图片完成长按");
    }
}

property

  • minimumPressDuration 最低长按时间
  • allowableMovement 长按时允许手指移动的距离

旋转

    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
    [self.viewNeedToRotate addGestureRecognizer: rotate];
    [rotate release];

- (void) rotateAction: (UIRotationGestureRecognizer *)rotate {
    // 通过手势,找到作用的视图
    UIImageView *viewNeedToRotate = (UIImageView *)rotate.view;
    // 对视图进行旋转
    self.viewNeedToRotate.transform = CGAffineTransformMakeRotation(rotate.rotation);
}

捏合

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.viewNeedToPinch addGestureRecognizer: pinch];
    [pinch release];

- (void) pinchAction: (UIPinchGestureRecognizer *)pinch {
    self.viewNeedToPinch.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}

拖拽

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

第一种方法


- (void) pan: (UIPanGestureRecognizer *) pan {
    // 手势经过的坐标
    CGPoint point = [pan translationInView:pan.view];
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
    [pan setTranslation:CGPointZero inView:pan.view];
}

第二种方法

- (void) pan: (UIPanGestureRecognizer *) pan {
    // 手势经过的坐标
    CGPoint point = [pan translationInView:pan.view];
    self.viewNeedToPan.center = CGPointMake(self.viewNeedToPan.center.x + point.x, self.viewNeedToPan.center.y +point.y);
    [pan setTranslation:CGPointZero inView:self.viewNeedToPan];
}

一般这样写

- (void) panAction: (UIPanGestureRecognizer *) pan {
    if (pan.state == UIGestureRecognizerStateBegan) {

    } else if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint point = [pan translationInView:pan.view];
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        [pan setTranslation:CGPointZero inView:pan.view];
    } else {
        // 返回原位
        pan.view.transform = CGAffineTransformIdentity;
    }
}

轻扫

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector(swipeAction:)];
    [self.viewNeedToSwipe addGestureRecognizer: swipe];
    [swipe release];

- (void) swipe: (UISwipeGestureRecognizer *) swipe {
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"left");
    } else {
        NSLog(@"other direction");
    }
}

property

  • direction 可以设置多个,前提是多个方向触发的行为要相同.如果多个方向触发的行为不同,要创建多个轻扫手势

屏幕边界手势

UIScreenEdgePanGestureRecognizer

你可能感兴趣的:(IOS基本控件)