事件手势,手势

来源:互联网 发布:湖人vs公牛球员数据 编辑:程序博客网 时间:2024/05/01 16:59

1.开始触摸事件:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"开始触摸%@", event);

}


2.正在触摸事件:

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"正在触摸%@", event);

}


3.取消触摸事件:

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 

    NSLog(@"取消触摸%@", event);

}


4.摇一摇开始事件:

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {    

    NSLog(@"摇一摇开始");

}


5.摇一摇结束事件:

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"摇一摇结束"); }


    二、手势

为了验证手势的作用,设置一张图片,在图片上做各种手势,看看图片的效果;

1.创建一个图片视图。并添加到根视图上。

UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

    imageview.center = self.view.center;

    imageview.image = [UIImage imageNamed:@"1.jpg"];

    imageview.userInteractionEnabled = YES;

    imageview.contentMode = UIViewContentModeScaleAspectFit;

    [self.view addSubview:imageview];

    

2.点击、轻拍(tap)

//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

////    执行方法需要点击(轻拍)的次数

//    tap.numberOfTapsRequired = 2;

////    需要几个手指点击(轻拍)

//    tap.numberOfTouchesRequired = 1;

////    将手势赋给视图

//    [imageview addGestureRecognizer:tap];


#pragma mark - 轻拍事件

- (void)tapAction:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍、点击手势");

//    NSLog(@"%@", tap.view);

    UIImageView *imageView = (UIImageView *)tap.view;

    [imageView setImage:[UIImage imageNamed:@"2.jpg"]];

}


3.长按(longPress)

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

////    判定为长按手势需要的时间 默认为0.5秒

//    longPress.minimumPressDuration = 2;

////  在判定过程中,允许用户手指移动的距离

//    longPress.allowableMovement = 200; 

//    [imageview addGestureRecognizer:longPress];


#pragma mark - 长按事件

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

    UIImageView *imageView = (UIImageView *)longPress.view;

    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];

}


4.捏合(pinch)

UIPinchGestureRecognizer *pinCh = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; 

    [imageview addGestureRecognizer:pinCh];


#pragma mark - 捏合事件

- (void)pinchAction:(UIPinchGestureRecognizer * )pinch {

    UIImageView *imageView = (UIImageView *)pinch.view;

    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1;

}


  5.拖拽/平移 (pan)

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

    [imageview addGestureRecognizer:pan];


#pragma mark - 拖拽/平移事件

- (void)panACtion:(UIPanGestureRecognizer *)pan {

    UIImageView *imageView = (UIImageView *)pan.view;

    CGPoint p = [pan translationInView:imageView];

//    NSLog(@"%f%f", p.x, p.y);

    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);

    [pan setTranslation:CGPointZero inView:imageView];

}


 6.旋转 (rotation)

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationACtion:)];

    [imageview addGestureRecognizer: rotation];


#pragma mark - 旋转事件

- (void)rotationACtion:(UIRotationGestureRecognizer *)rotation {

 UIImageView *imageView = (UIImageView *)rotation.view;

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);

    rotation.rotation = 0;

}


7.轻扫手势(swipe)

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeACtion:)];

    [imageview addGestureRecognizer:swipe];


#pragma mark - 轻扫手势事件

- (void)swipeACtion:(UISwipeGestureRecognizer *)swipe { 

    UIImageView *imageView = (UIImageView *)swipe.view;

}


 8.屏幕边缘轻扫识别器(screenEdgePan)

    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanACtion:)];


#pragma mark - 屏幕边缘轻扫手势

- (void)screenEdgePanACtion:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {

    UIImageView *imageView = (UIImageView *)screenEdgePan.view;

}

0 0
原创粉丝点击