Iphone开发中的手势(点击,移动,…

来源:互联网 发布:unity3d t4m 编辑:程序博客网 时间:2024/04/30 13:33

 

//创建要操作的图片视图

 imageView [[UIImageView allocinitWithFrame:CGRectMake(00100100)];//成员变量

    finSize CGSizeMake(100100);//成员变量,图片大小

    imageView.image [UIImage imageNamed:@"5.png"];

    [self.view addSubview:imageView];

    [imageView release];

    imageView.userInteractionEnabled YES;//支持交互

    

//点击手势

    //创建一个点击手势识别器对象

    UITapGestureRecognizertap [[UITapGestureRecognizer allocinitWithTarget:self action:@selector(tap:)];

    //点击次数,当连续点击2次时,触发函数

    tap.numberOfTapsRequired 2;

    //多点点击,当2个点同时触控时,触发函数

    tap.numberOfTouchesRequired 2;

    [imageView addGestureRecognizer:tap];//给图片视图绑定手势识别器对象

    [tap release];

    

//移动手势

    UIPanGestureRecognizerpan [[UIPanGestureRecognizer allocinitWithTarget:self action:@selector(pan:)];

    [imageView addGestureRecognizer:pan];

    [pan release];

    

//缩放

    UIPinchGestureRecognizerpinch [[UIPinchGestureRecognizer allocinitWithTarget:self action:@selector(pinch:)];

    [imageView addGestureRecognizer:pinch];

    [pinch release];

    

//旋转

    UIRotationGestureRecognizerrotation [[UIRotationGestureRecognizer allocinitWithTarget:self action:@selector(rotation:)];

    [imageView addGestureRecognizer:rotation];

    [rotation release];

 

//触发函数    

 

//轻触

(void)tap:(UITapGestureRecognizer*)tap

{

     imageView.frame=CGRectMake(60, 130, 200,200);
     [self.viewbringSubviewToFront:imageView];

}

 

//移动

(void)pan:(UIPanGestureRecognizer*)pan

{

    //得到偏移坐标

    CGPoint point [pan translationInView:self.view];

    imageView.center CGPointMake(imageView.center.x point.ximageView.center.y point.y);

    [pan setTranslation:CGPointMake(00inView:self.view];//偏移量归零

}

//缩放

(void)pinch:(UIPinchGestureRecognizer*)pinch

{

    imageView.bounds CGRectMake(00finSize.width pinch.scalefinSize.height pinch.scale);//pinch.scale是缩放的倍数

    

    if (pinch.state == UIGestureRecognizerStateEnded

   {//操作停止后,更新原图片视图大小

        finSize CGSizeMake(finSize.width pinch.scalefinSize.height pinch.scale);

    }

}

 

//旋转

(void)rotation:(UIRotationGestureRecognizer*)rotation

{

    imageView.transform CGAffineTransformMakeRotation(finRotation rotation.rotation);

//finRotation为成员变量(float),初始值为0,rotation.rotation为改变的弧度

    if (rotation.state == UIGestureRecognizerStateEnded   {

        finRotation += rotation.rotation;

    }

}

原创粉丝点击