Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIPanGestureRecognizer的使用_Iphone_Objective C_Uigesturerecognizer_Uipangesturerecognizer - Fatal编程技术网

Iphone UIPanGestureRecognizer的使用

Iphone UIPanGestureRecognizer的使用,iphone,objective-c,uigesturerecognizer,uipangesturerecognizer,Iphone,Objective C,Uigesturerecognizer,Uipangesturerecognizer,我有一个使用UIPangestureRecognitor在屏幕上移动的小图像视图,但是当它从superview的边缘出来时,不会得到更多的触摸,因此无法再返回。如何防止图像从superview的边缘出来? 代码如下: - (void)pan:(UIPanGestureRecognizer *)gestureRecognizer { if (inLongPress) { UIView *hitView = [gestureRecognizer view]; [self adjus

我有一个使用UIPangestureRecognitor在屏幕上移动的小图像视图,但是当它从superview的边缘出来时,不会得到更多的触摸,因此无法再返回。如何防止图像从superview的边缘出来? 代码如下:

- (void)pan:(UIPanGestureRecognizer *)gestureRecognizer
{
if (inLongPress) {
    UIView *hitView = [gestureRecognizer view];

    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if (!(CGRectContainsRect(self.imageView.frame, hitView.frame))) {

        [UIView beginAnimations:@"" context:NULL];
        [hitView setCenter:CGPointMake(_prevCenter.x, _prevCenter.y)];
        [UIView commitAnimations];

     }
    _prevCenter=hitView.center;

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

        [hitView setCenter:CGPointMake([hitView center].x + translation.x, [hitView center].y + translation.y)];

        [gestureRecognizer setTranslation:CGPointZero inView:[hitView superview]];
    }

    if ([gestureRecognizer state] == UIGestureRecognizerStateEnded){
        inLongPress=NO;
        hitView.alpha=1.0;
    }
}

}

在视图控制器的viewDidLoad方法中,只需将superview的ClipToBounds属性设置为YES


[superview setClipsToBounds:是]

在视图控制器的viewDidLoad方法中,只需将superview的clipsToBounds属性设置为YES

[superview setClipsToBounds:是]

如果将CGPoint限制在允许范围内,只需使用两个

类似这样的操作可确保无法将对象移动到视图之外:

CGFloat proposedX = location.x;
CGFloat proposedY = location.y;

CGRect objectFrame = hitView.frame;
CGFloat allowedXMin = 0 + objectFrame.size.width / 2.0f;
CGFloat allowedXMax = self.view.bounds.size.width - objectFrame.size.width / 2.0f;
CGFloat allowedYMin = 0 + objectFrame.size.height / 2.0f;
CGFloat allowedYMax = self.view.bounds.size.height - objectFrame.size.height / 2.0f;

if (proposedX < allowedXMin) {
    proposedX = allowedXMin;
}
else if (proposedX > allowedXMax) {
    proposedX = allowedXMax;
}

if (proposedY < allowedYMin) {
    proposedY = allowedYMin;
}
else if (proposedY > allowedYMax) {
    proposedY = allowedYMax;
}
location = CGPointMake(proposedX, proposedY);

hitView.center = location;
如果这将CGPoint限制在允许的范围内,只需使用几个

类似这样的操作可确保无法将对象移动到视图之外:

CGFloat proposedX = location.x;
CGFloat proposedY = location.y;

CGRect objectFrame = hitView.frame;
CGFloat allowedXMin = 0 + objectFrame.size.width / 2.0f;
CGFloat allowedXMax = self.view.bounds.size.width - objectFrame.size.width / 2.0f;
CGFloat allowedYMin = 0 + objectFrame.size.height / 2.0f;
CGFloat allowedYMax = self.view.bounds.size.height - objectFrame.size.height / 2.0f;

if (proposedX < allowedXMin) {
    proposedX = allowedXMin;
}
else if (proposedX > allowedXMax) {
    proposedX = allowedXMax;
}

if (proposedY < allowedYMin) {
    proposedY = allowedYMin;
}
else if (proposedY > allowedYMax) {
    proposedY = allowedYMax;
}
location = CGPointMake(proposedX, proposedY);

hitView.center = location;