0
-(void)scale:(id)sender {

[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

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

    lastScale = 1.0;
    return;
}

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

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

-(void)rotate:(id)sender {

[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

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

    lastRotation = 0.0;
    return;
}

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

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

-(void)move:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
}

translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

[[sender view] setCenter:translatedPoint];

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

    CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

    if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {

        if(finalX < 0) {                                finalX = 0;             }                       else if(finalX > 768) {

            finalX = 768;
        }

        if(finalY < 0) {                                finalY = 0;             }                       else if(finalY > 1024) {

            finalY = 1024;
        }
    }

    else {

        if(finalX < 0) {                                finalX = 0;             }                       else if(finalX > 1024) {

            finalX = 768;
        }

        if(finalY < 0) {                                finalY = 0;             }                       else if(finalY > 768) {

            finalY = 1024;
        }
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [[sender view] setCenter:CGPointMake(finalX, finalY)];
    [UIView commitAnimations];
}
}

-(void)tapped:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer      shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

这是我的代码,我要移动的 UIImageView 称为“Birdie”。我知道我必须创建一个 -(void)Birdie UIImageView 识别器,但我使用了以下没有用的方法。

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

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

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

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[Birdie addGestureRecognizer:tapRecognizer];

[self.view addSubview:Birdie];
}

非常感谢先进!

4

1 回答 1

-1

您需要在界面中放入一些值,例如:

//Zoom
CGFloat lastScaleFactor;

//Movement
CGPoint startLocation;
float distance;

//Rotation
CGFloat valore;

然后:

- (void)zoomAction:(UIGestureRecognizer *)sender {

    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    if (factor > 1) {

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1), lastScaleFactor + (factor-1));

    } else {

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor);

    }

    //Save the new "scale" factor
    if (sender.state == UIGestureRecognizerStateEnded) { 
        if (factor > 1) {
            lastScaleFactor += (factor-1); 
        } else {
            lastScaleFactor *= factor;
        }
    }
}
- (void)panAction:(UIPanGestureRecognizer *)gesture {
    if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) {

        CGPoint location = [gesture locationInView:self.view];

        [self.yourImage setCenter:location];
    }
}

- (void)rotationAction:(UIRotationGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
         [gesture setRotation:valore * 10];
    }
    if (gesture.state == UIGestureRecognizerStateChanged) {
        valore = gesture.rotation / 10;
        if (valore > 1.0) valore = 1.0;
        if (valore < 0.2) valore = 0.2;

        self.yourImage.alpha = valore;
    }
    NSLog(@"rotazione %f", gesture.rotation / 10);
}

在这种情况下,我使用旋转来更改 alpha,但你可以做你需要的

于 2011-09-02T09:28:23.110 回答