UI手势

 

#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //自定义一张图片

    UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 300, 300)];

    

    picture.image = [UIImage imageNamed:@"053e01451f7f39e5d4c53d20d8998672.jpg"];

    

    picture.userInteractionEnabled = YES;

    

    [self.view addSubview:picture];

    

    [picture release];

        

                 /*******************  手势  ******************/

    

    //轻拍(Tap)

    /*

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

    

    //设置轻拍次数 (默认1)

    tap.numberOfTapsRequired = 2;

    

    //设置轻拍的手指个数 (默认1)

    tap.numberOfTouchesRequired = 2;

    

    [picture addGestureRecognizer:tap];

    [tap release];

    */

    

    /*

    //轻扫(Swipe)

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

    

    //设置轻扫方向

#warning 注意: 水平或者垂直方向只能选择一个方向.

    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight ;

    

    //设置轻扫手指个数

    swipe.numberOfTouchesRequired = 2;

    

    [self.view addGestureRecognizer:swipe];

    

    [swipe release];

    */

    

    /*

    //捏合(Pich)

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

    

    

    [self.view addGestureRecognizer:pinch];

    [pinch release];

    */

    

    /*

    //旋转(Rotation)

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

    

    [picture addGestureRecognizer:rotation];

    

    [rotation release];

    */

    

    /*

    //平移(Pan)

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

    

    [picture addGestureRecognizer:pan];

    

    [pan release];

    */

    

    //长按 (LongPress)

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

    

    //设置长按时间

    longPress.minimumPressDuration = 2;

    

    [picture addGestureRecognizer:longPress];

    

    [longPress release];

    

}


-(void)tapAction:(UITapGestureRecognizer *)tap

{

    NSLog(@"被轻拍");

}



-(void)swipeAction:(UISwipeGestureRecognizer *)swipe

{

    NSLog(@"轻扫");

}



-(void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    //放大缩小图片

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

    

//    pic.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);

    

    //参数说明: 1.当前视图的transform 2.width的缩放比例 3.height的缩放比例

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

    

    //缩放结束之后, 将缩放比例置为1.0

    pinch.scale = 1.0;

    

    NSLog(@"捏合");

}


-(void)rotationAction:(UIRotationGestureRecognizer *)rotation

{

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

    

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

    

    //将当前旋转位置置为零度

    rotation.rotation = 0.0;

    

    NSLog(@"旋转");

}


-(void)panAction:(UIPanGestureRecognizer *)pan

{

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

    

    CGPoint p = [pan translationInView:pic];

    

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

    

//    [pan setTranslation:CGPointMake(0, 0) inView:pic];

    //将当前位置, 设置成起始位置

    [pan setTranslation:CGPointZero inView:pic];

    

    NSLog(@"平移");

}



-(void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

    

    NSLog(@"长按");

    

    //长按时刻

    if (longPress.state == UIGestureRecognizerStateBegan) {

        

        [longPress.view removeFromSuperview];


    }

  

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值