手势识别器

(1). UIImageView设置成属性

@property(nonatomic, retain)UIImageView *inmageView;



 (2)创建

UIImage *image = [UIImage imageNamed:@"psu.jpeg"];

self.inmageView = [[UIImageView alloc]initWithImage:image];

self.inmageView.frame = CGRectMake(100, 300, 200, 200);

[self.view addSubview:self.inmageView];

[self.inmageView release];

    

    

(3)把图片的用户交互打开,它默认是关闭的(原先是不可操作的,现在让他变成可以操作的),此外还有一个控件是label

 self.inmageView.userInteractionEnabled = YES;


手势的使用

(1).点击

创建

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

   

设置点击几次才能触发方法

tap.numberOfTapsRequired = 2;

    

设置几根手指进行点击

tap.numberOfTouchesRequired = 2;


将手势添加到对应的图片上

[self.inmageView addGestureRecognizer:tap];

释放

[tap release];


点击  图片切换图片

-(void)tapAction:(UITapGestureRecognizer *)tap

{

    NSLog(@"测试一下点击手势");

    self.inmageView.image = [UIImage imageNamed:@"580.jpeg"];

}



(2).长按

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

    

设置长按触发的最小时间

longPress.minimumPressDuration = 0.5;

    

允许用户手指在长按过程中允许移动的距离

longPress.allowableMovement = 200;

    

把手势添加到图片上

[self.inmageView addGestureRecognizer:longPress];

[longPress release];


长按之后弹出 UIAlertView

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

    NSLog(@"测试滑动手势");

长按的状态

     longPress.state

    为了避免重复创建

    if (!self.alert) {如果当前是空得才创建这样就不会出现两次了

        self.alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你是谁:" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

        [self.alert release];

        [self.alert show];

    } else {

        [self.alert show]; 为了第二次长按

    }

}


(3).旋转

    创建一个旋转的手势

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

    

    把手是方法对应的图片上

    [self.inmageView addGestureRecognizer:rotation];

    

    释放

    [rotation release];


通过旋转手势 把图片旋转

-( void )rotationAction:( UIRotationGestureRecognizer *)rotation

{

    可以通过手势获取手势添加的视图是哪一个;

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

    

    进行旋转的操作

    

    通过视图的transform属性, 让视图进行旋转

    imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);

    

    防止旋转过程中停止

    rotation.rotation = 0;

}



(4).捏合

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

    [self.inmageView addGestureRecognizer:pinch];

    [pinch release];


通过捏合改变视图尺寸

-(void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    通过手势获取对应的视图

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

    

    通过视图的transform属性,改变视图尺寸

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

    

    终止尺寸,为了防止手势的变化让图片直接消失

    pinch.scale = 1;

}



(5).拖拽

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

    [self.inmageView addGestureRecognizer:pan];

    [pan release];


通过拖拽手势, 让视图随着手势的移动而移动

-(void)panAction:(UIPanGestureRecognizer *)pan

{

 通过手势获取视图

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

    

通过手势获得经过的点

    CGPoint p = [pan translationInView:imageView];

    

设置移动的位置

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

    

为了防止手势在操作的时候视图消失(移动的太快,无法控制)

    [pan setTranslation:CGPointZero inView:imageView];    

}




(6).清扫

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

    [self.inmageView addGestureRecognizer:swipe];

    [swipe release];

    

设置清扫的方向

    swipe.direction = UISwipeGestureRecognizerDirectionLeft ;








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值