在iphone中对图像视图实现捏合效果

时间:2011-07-14 13:16:13

标签: iphone uigesturerecognizer touch-event

提前致谢。    我想在图像视图上应用捏合,旋转和平移手势效果。像这样

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scalePiece:)];
    [imgView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotatePiece:)];
    [imgView addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panPiece:)];
    [imgView addGestureRecognizer:panGesture];
    [panGesture 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]);
         NSLog(@"Rotate : %f",[gestureRecognizer rotation]);
        [gestureRecognizer setRotation:0];
    }
}
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *piece = [gestureRecognizer view];

    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
    }
}

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

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

在此旋转和滑动(平移)工作正常但捏不好。我看过一个应用程序,即http://itunes.apple.com/us/app/image-mask-costume-hd/id443821357?mt = 8,触摸事件非常好。我希望通过使用手势或者我们需要跟踪触摸事件来实现这样的可能性。有没有人有想法请帮我做。

2 个答案:

答案 0 :(得分:2)

试试此代码:http://dl.dropbox.com/u/9397784/Image%20multitouch.txt

currentlyEditing替换为您的观点。只需将代码复制到要使用它的视图或视图控制器的实现中。代码处理缩放,旋转和平移。要将视图重置回默认位置,请致电yourView.transform = CGAffineTransformIdentity;

答案 1 :(得分:0)

Apple Touches example建议使用[gestureRecognizer scale]而非[gestureRecognizer velocity]

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

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