2016-02-29 85 views
0

我使用UIBezierPath如下在tringular形狀掩蔽的UIView(240 * 240):限制UIPanGestureRecognizer在UIView的面膜

path = [UIBezierPath new]; 
[path moveToPoint:(CGPoint){0, 240}]; 
[path addLineToPoint:(CGPoint){120,0}]; 
[path addLineToPoint:(CGPoint){240,240}]; 
[path addLineToPoint:(CGPoint){0,240}]; 
[path closePath]; 

CAShapeLayer *mask = [CAShapeLayer new]; 
mask.frame = self.viewShape.bounds; 
mask.path = path.CGPath; 
self.viewShape.layer.mask = mask; 

enter image description here

在上述圖像三角形區域是掩模。現在我有了可口可樂的形象,只能用三角形面具移動。所以,爲此,我已經應用UIPanGestureRecognizerUIIMageView並按以下方式限制其框架。

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint touchLocation = [gestureRecognizer locationInView:self.viewShape]; 

    CGRect boundsRect; 

    BOOL isInside = [path containsPoint:CGPointMake(self.innerView.center.x, self.innerView.center.y)]; 
    NSLog(@"value:%d",isInside); 
    if (isInside) { 
     NSLog(@"inside"); 
     self.innerView.center = touchLocation; 
    }else{ 
     NSLog(@"outside"); 
    } 
} 

我上面如果條件執行成功,但是當控制進入其他條件,我不能拖回到面罩框架內我的ImageView。

所以,我的問題是當其他塊(外部)調用我應該能夠再次拖動imageView內的面具的框架。

我該如何做到這一點?

回答

0

我修改了一部分Meth的代碼。代碼如下:

self.innerView.center = touchLocation; 
BOOL isInside = [path containsPoint:CGPointMake(self.innerView.center.x, self.innerView.center.y)]; 
if (isInside) { 
     NSLog(@"inside"); 
     self.innerView.center = touchLocation; 
     lastValidCenter = self.innerView.center; 
    }else{ 
     NSLog(@"outside"); 
     self.innerView.center = lastValidCenter; 
    } 
0

保存引用到imageview的最後中心是實現此目的的一種方法。 在你的customView;

CGPoint lastValidCenter; //initially it is the center of imageview; 

,並在代碼

NSLog(@"value:%d",isInside); 
    if (isInside) { 
     NSLog(@"inside"); 
     self.innerView.center = touchLocation; 
     lastValidCenter = self.innerView.center; 
    }else{ 
     NSLog(@"outside"); 
     self.innerView.center = lastValidCenter; 
    } 
+0

感謝您的回覆。我做了這樣的實現,但得到了相同的結果。 – iPhone

0

有一個在你的計算問題。你需要做的是檢查UIImageView的左上角和右上角的包含點UIBezirePath實例。 這是因爲當圖像移動時,這兩個角落都是先嚐試出去的角落。所以,只需支票,你就可以得到你想要的結果。

BOOL isInside = [path containsPoint:CGPointMake(CGRectGetMinX(self.innerView.bounds), CGRectGetMinY(self.innerView.bounds))]; 
isInside = isInside || [path containsPoint:CGPointMake(CGRectGetMaxX(self.innerView.bounds), CGRectGetMinY(self.innerView.bounds))]; 
if (isInside) { 
//Move your UIImageView 
} 
else { 
//Don't move 
}