我如何阻止UIImageView被移动,在UIView之外使用GestureRecongnizers?

时间:2011-12-19 16:22:33

标签: uiview uiviewcontroller uiimageview uiimage uigesturerecognizer

我正在创建一个基于图像的应用程序,我希望显示的图像在我创建的UIView中旋转/移动/缩放。我在网上关于如何实现UIGestures的一个有用的教程,我已经掌握了使用它们,我只需要了解它们背​​后的代码,以便我可以在这个UIView中保留手势。这是代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
layoutOne = [[UIView alloc] initWithFrame:CGRectMake(95.0, 107.0, 578, 682)];
theImageView = [[UIImageView alloc] initWithFrame:[layoutOne frame]];
[theImageView setImage:image];
theImageView.userInteractionEnabled = TRUE;
[layoutOne addSubview:theImageView];
takePhoto.hidden = YES;
theImageView.clipsToBounds = YES;

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

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

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

UITapGestureRecognizer *deleteButton = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(delButton:)];
[deleteButton setNumberOfTapsRequired:2];
[deleteButton setDelegate:self];
[theImageView addGestureRecognizer:deleteButton];


UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[tapRecognizer requireGestureRecognizerToFail:deleteButton];
[theImageView addGestureRecognizer:tapRecognizer];
[self.view addSubview:theImageView];
}

注意

UIImage *image;
UIImageView *theImageView;
UIView *layoutOne;

这些是我宣布的手势识别器,下面是我遵循的教程的另一部分,以帮助他们完成工作:

-(void)scale:(id)sender {
NSLog(@"See a pinch gesture");
[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 {
    NSLog(@"See a rotate gesture");
    [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 {
    NSLog(@"See a move gesture");
    [[(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)move:(id)sender内进行的数学运算,这样我就可以阻止UIImage / UIImageView移动到我创建的UI子视图(layoutOne)之外。如果你能帮助我,我将非常感激!感谢。

1 个答案:

答案 0 :(得分:2)

我想通过上面使用的方法来解决这个问题。一旦我弄明白我需要做什么,这真的很容易。如果您自己使用上述方法,那么您需要做的就是确保UIView您使用的layoutOne被称为move:。在rotate:scale:layoutOne.clipsToBounds = YES;方法中,您需要在if中添加,以确保它不在else ifelse中,UIView陈述。这应该保持您想要与之交互的图像不会移动到{{1}}之外,如果它接触到视图的边界,则图像的这些部分将不会出现。 Simples!