Fork me on GitHub

UIImageView实现图片移动,缩放、旋转的代码片段

继承UIImageView,重写init函数。

//旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer allor]initWithTarget:selft action:@selector(rotatePiece:)];
[self addGestureRecognizer:rotationGesture];
[rotationGesture release];

//放大缩小手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scalePiece:)];
[pinchGesture setDelegate:self];
[self addGestureRecognizer:pinchGesture];
[pinchGesture release];

 

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}

- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
rotate = [gestureRecognizer rotation];
isMoveState = NO;
[gestureRecognizer setRotation:0];
}
}

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
scale = [gestureRecognizer scale];
isMoveState = NO;
[gestureRecognizer setScale:1];
}
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer.view != self.view)
return NO;

if (gestureRecognizer.view != otherGestureRecognizer.view)
return NO;

if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
return NO;

return YES;
}

 

//移动方法,仍是继承UIImageView重写Touch
initialPoint为全局CGPoint

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

if ([touch tapCount] == 1)
{
CGPoint currentPoint = [touch locationInView:self];

if (isOne)
{
initialPoint = currentPoint;
isOne = NO;
}
CGFloat offsetX = currentPoint.x + self.frame.origin.x - initialPoint.x;
CGFloat offsetY = currentPoint.y + self.frame.origin.y - initialPoint.y;
self.frame = CGRectMake(offsetX, offsetY, self.frame.size.width, self.frame.size.height);
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

initialPoint = CGPointMake(0, 0);
isOne = YES;
}





posted on 2012-03-06 13:22  pengyingh  阅读(1481)  评论(0)    收藏  举报

导航