手势识别器

来源:互联网 发布:传世登陆器源码 编辑:程序博客网 时间:2024/06/13 03:25

(1). UIImageView设置成属性

@property(nonatomic,retain)UIImageView *inmageView;



 (2)创建

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

self.inmageView = [[UIImageViewalloc]initWithImage:image];

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

[self.viewaddSubview:self.inmageView];

[self.inmageViewrelease];

    

    

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

 self.inmageView.userInteractionEnabled =YES;


手势的使用

(1).点击

创建

UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapAction:)];

   

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

tap.numberOfTapsRequired =2;

    

设置几根手指进行点击

tap.numberOfTouchesRequired = 2;


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

[self.inmageViewaddGestureRecognizer:tap];

释放

[tap release];


点击 图片切换图片

-(void)tapAction:(UITapGestureRecognizer *)tap

{

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

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

}



(2).长按

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressAction:)];

    

设置长按触发的最小时间

longPress.minimumPressDuration =0.5;

    

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

longPress.allowableMovement =200;

    

把手势添加到图片上

[self.inmageViewaddGestureRecognizer:longPress];

[longPress release];


长按之后弹出 UIAlertView

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

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

长按的状态

     longPress.state

   为了避免重复创建

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

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

        [self.alertrelease];

        [self.alertshow];

    } else {

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

    }

}


(3).旋转

    创建一个旋转的手势

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationAction:)];

    

   把手是方法对应的图片上

    [self.inmageViewaddGestureRecognizer:rotation];

    

    释放

    [rotation release];


通过旋转手势把图片旋转

-(void)rotationAction:(UIRotationGestureRecognizer *)rotation

{

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

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

    

    进行旋转的操作

    

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

    imageView.transform =CGAffineTransformMakeRotation(rotation.rotation);

    

    防止旋转过程中停止

    rotation.rotation =0;

}



(4).捏合

   UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];

    [self.inmageViewaddGestureRecognizer:pinch];

    [pinchrelease];


通过捏合改变视图尺寸

-(void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    通过手势获取对应的视图

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

    

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

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

    

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

   pinch.scale =1;

}



(5).拖拽

   UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];

    [self.inmageViewaddGestureRecognizer:pan];

    [panrelease];


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

-(void)panAction:(UIPanGestureRecognizer *)pan

{

 通过手势获取视图

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

    

通过手势获得经过的点

    CGPoint p = [pan translationInView:imageView];

    

设置移动的位置

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

    

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

    [pansetTranslation:CGPointZeroinView:imageView];    

}




(6).清扫

   UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeAction:)];

    [self.inmageViewaddGestureRecognizer:swipe];

    [swiperelease];

    

设置清扫的方向

    swipe.direction =UISwipeGestureRecognizerDirectionLeft ;








0 0
原创粉丝点击