//创建要操作的图片视图
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];//成员变量
finSize = CGSizeMake(100, 100);//成员变量,图片大小
imageView.image = [UIImage imageNamed:@"5.png"];
[self.view addSubview:imageView];
[imageView release];
imageView.userInteractionEnabled = YES;//支持交互
//点击手势
//创建一个点击手势识别器对象
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//点击次数,当连续点击2次时,触发函数
tap.numberOfTapsRequired = 2;
//多点点击,当2个点同时触控时,触发函数
tap.numberOfTouchesRequired = 2;
[imageView addGestureRecognizer:tap];//给图片视图绑定手势识别器对象
[tap release];
//移动手势
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[imageView addGestureRecognizer:pan];
[pan release];
//缩放
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
//旋转
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget: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.x, imageView.center.y + point.y);
[pan setTranslation:CGPointMake(0, 0) inView:self.view];//偏移量归零
}
//缩放
- (void)pinch:(UIPinchGestureRecognizer*)pinch
{
imageView.bounds = CGRectMake(0, 0, finSize.width * pinch.scale, finSize.height * pinch.scale);//pinch.scale是缩放的倍数
if (pinch.state == UIGestureRecognizerStateEnded)
{//操作停止后,更新原图片视图大小
finSize = CGSizeMake(finSize.width * pinch.scale, finSize.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;
}
}