下手势UIGestureRecognizer对图像进行缩放、移动、旋转操作

于视图中开创手势识别器UIGestureRecognizers

– (void)viewDidLoad  

// 缩放
-(void)scale:(id)sender {

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
      _lastScale = 1.0;
    }

    CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [photoImage setTransform:newTransform];

    _lastScale = [(UIPinchGestureRecognizer*)sender scale];
    [self showOverlayWithFrame:photoImage.frame];
}

// 旋转
-(void)rotate:(id)sender {

    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

      _lastRotation = 0.0;
      return;
    }

    CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [photoImage setTransform:newTransform];

    _lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
    [self showOverlayWithFrame:photoImage.frame];
}

// 移动
-(void)move:(id)sender {

  CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:canvas];

  if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
    _firstX = [photoImage center].x;
    _firstY = [photoImage center].y;
  }

  translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);

  [photoImage setCenter:translatedPoint];
  [self showOverlayWithFrame:photoImage.frame];
}

盖路用,需要针对图片进行局部操作。本着拿来主义的基准~上网找了主意。但是发现网上广大贯彻,要么过于复杂,要么不敷完善。终于找到同样首比较好的章。

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
    [pinchRecognizer setDelegate:self];
    [self.view addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] autorelease];
    [rotationRecognizer setDelegate:self];
    [self.view addGestureRecognizer:rotationRecognizer];

    UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [canvas addGestureRecognizer:panRecognizer];

    UITapGestureRecognizer *tapProfileImageRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)] autorelease];
    [tapProfileImageRecognizer setNumberOfTapsRequired:1];
    [tapProfileImageRecognizer setDelegate:self];
    [canvas addGestureRecognizer:tapProfileImageRecognizer];
}

if (showImgView.frame.size.width < oldFrame.size.width)

iOS提供了拍击、旋转、滑动、挤捏等丰富的手势,因此对图像的操作就显得异常好、简单。下面代码展示了使UIGestureRecognizer对图像进行缩放
、移动、旋转操作以办法。在动代码之前,首先成立一个UIView,并以该视图中加进一个UIImageView视图,用于展示图像。那么该UIView就类似图
像画板一样,对图像的操作都冲这视图中开展。http://www.ctolive.com/space-1023-do-blog-id-2109.html

 – (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer 

脚是应各手势消息的艺术:

  – (void) addGestureRecognizerToView:(UIView *)view 

{         

CGFloat lastScale;     

 {              

pinchGestureRecognizer.scale = 1;      

CGRect largeFrame;  //确定图片放大最特别之水平  

 // 旋转手势    

oldFrame = showImgView.frame;      

// 添加所有的手势

{          

 }   

   if (panGestureRecognizer.state == UIGestureRecognizerStateBegan ||
panGestureRecognizer.state == UIGestureRecognizerStateChanged)

}  

[self addGestureRecognizerToView:view];     

 CGRect oldFrame;    //保存图片原来的高低      

 [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y

切切实实用一下:在.h文件之中定义变量

 [showImgView setMultipleTouchEnabled:YES];      

 // 处理旋转手势  

这么即使好了。保证了图片的极可怜与太小比例。

}    

 {      

 {      

参考文章: http://apluck.iteye.com/blog/1781607

// 处理缩放手势 

 – (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer  

 – (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer 

这么虽落实了而是,这样是不够的。因为里面的缩放和运动等没有开相应的判断。因为代码很简短,所以扩展为特别便宜。我修改了缩放的代码,增加了限制,其他的切近

if (showImgView.frame.size.width > 3 * oldFrame.size.width) 

 [view addGestureRecognizer:rotationGestureRecognizer];           

[showImgView setImage:[UIImage imageNamed:@”1.jpg”]];       

如此单待简单调用

 view.transform = CGAffineTransformScale(view.transform,
pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);         
pinchGestureRecognizer.scale = 1;     

 [view addGestureRecognizer:panGestureRecognizer]; 

 //如果处理的是图形,别忘了  

}

}          

接下来于viewDidLoad里面添加

{      

showImgView.frame = oldFrame;              

showImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320,
480)];     

 UIView *view = panGestureRecognizer.view;   

 [imageView setMultipleTouchEnabled:YES]; 

 // 缩放手势     

 UIPanGestureRecognizer *panGestureRecognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(panView:)];     

 }  

[imageView setUserInteractionEnabled:YES];   

}  

if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan ||
pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) 

// 处理缩放手势 

UIView *view = pinchGestureRecognizer.view;      

 // 处理拖拉手势 

{         

   // 移动手势

}  

[self.view addSubview:showImgView];  

[super viewDidLoad];        

//让图片无法缩得比原来图小          

 }  

showImgView.frame = largeFrame;          

{              

  UIRotationGestureRecognizer *rotationGestureRecognizer =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:@selector(rotateView:)];     

UIView *view = pinchGestureRecognizer.view;      

largeFrame = CGRectMake(0 – screenSize.width, 0 – screenSize.height, 3
* oldFrame.size.width, 3 * oldFrame.size.height);           

  [panGestureRecognizer setTranslation:CGPointZero
inView:view.superview];      } 

 {     

{      

UIView *view = rotationGestureRecognizer.view;    

– (void) rotateView:(UIRotationGestureRecognizer
*)rotationGestureRecognizer  {      

  • translation.y}];        

 CGPoint translation = [panGestureRecognizer
translationInView:view.superview];      

}   

if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan ||
pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) 

@interface YourViewController : UIViewController

[view addGestureRecognizer:pinchGestureRecognizer];         

 [rotationGestureRecognizer setRotation:0];     

 UIPinchGestureRecognizer *pinchGestureRecognizer =
[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(pinchView:)];      

  if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan
|| rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) 

 大功告成。

{     

 [self addGestureRecognizerToView:showImgView];      

[showImgView setUserInteractionEnabled:YES];    

 }  

}          

view.transform = CGAffineTransformScale(view.transform,
pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);          

 {         

 view.transform = CGAffineTransformRotate(view.transform,
rotationGestureRecognizer.rotation);         

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图