0

使用我碰到的一些代码在图像上绘制一个圆形。平移和捏手势用于绘制的圆形。形状的移动跨过UIImageView的尺寸,到达不显示图像的其余UI。我试图将其他示例中使用的方法合并到需要平移和缩放限制以限制圆形形状移动到UIImageView边界的方法中,但无法让它们工作。我知道我需要控制pan和pinch的中心和半径值,以便它们不会跨越UIImageView的边界,但对于如何执行此操作没有丝毫的线索。附件是我正在使用的代码。谢谢你的帮助!限制形状移动到UIImageView的尺寸

-(void)setUpCropTool:(id)sender{ 

    // create layer mask for the image 

    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    self.imageToBeCropped.layer.mask = maskLayer; 
    self.maskLayer = maskLayer; 

    // create shape layer for circle we'll draw on top of image (the boundary of the circle) 

    CAShapeLayer *circleLayer = [CAShapeLayer layer]; 
    circleLayer.lineWidth = 50; 
    circleLayer.fillColor = [[UIColor clearColor] CGColor]; 
    circleLayer.strokeColor = [[UIColor redColor] CGColor]; 
    [self.imageToBeCropped.layer addSublayer:circleLayer]; 
    self.circleLayer = circleLayer; 

    // create circle path 

    [self updateCirclePathAtLocation:CGPointMake(self.imageToBeCropped.bounds.size.width/2.0, self.imageToBeCropped.bounds.size.height/2.0) radius:self.imageToBeCropped.bounds.size.width/2.5]; 

    // create pan gesture 

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    pan.delegate = self; 
    [self.imageToBeCropped addGestureRecognizer:pan]; 
    self.imageToBeCropped.userInteractionEnabled = YES; 
    self.pan = pan; 

    // create pan gesture 

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; 
    pinch.delegate = self; 
    [self.imageToBeCropped addGestureRecognizer:pinch]; 
    self.pinch = pinch; 

} 

- (void)updateCirclePathAtLocation:(CGPoint)location radius:(CGFloat)radius 
{ 
    self.circleCenter = location; 
    self.circleRadius = radius; 

    UIBezierPath *path = [UIBezierPath bezierPath]; 
    [path addArcWithCenter:self.circleCenter 
        radius:self.circleRadius 
       startAngle:0.0 
        endAngle:M_PI * 2.0 
       clockwise:YES]; 

    self.maskLayer.path = [path CGPath]; 
    self.circleLayer.path = [path CGPath]; 
} 

#pragma mark - Gesture recognizers 

- (void)handlePan:(UIPanGestureRecognizer *)gesture 
{ 
    static CGPoint oldCenter; 
    CGPoint tranlation = [gesture translationInView:gesture.view]; 

    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     oldCenter = self.circleCenter; 
    } 

    CGPoint newCenter = CGPointMake(oldCenter.x + tranlation.x, oldCenter.y + tranlation.y); 



     [self updateCirclePathAtLocation:newCenter radius:self.circleRadius]; 



} 

- (void)handlePinch:(UIPinchGestureRecognizer *)gesture 
{ 
    static CGFloat oldRadius; 
    CGFloat scale = [gesture scale]; 

    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     oldRadius = self.circleRadius; 
    } 

    CGFloat newRadius = oldRadius * scale; 

    [self updateCirclePathAtLocation:self.circleCenter radius:newRadius]; 
} 


#pragma mark - UIGestureRecognizerDelegate 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    if ((gestureRecognizer == self.pan && otherGestureRecognizer == self.pinch) || 
     (gestureRecognizer == self.pinch && otherGestureRecognizer == self.pan)) 
    { 
     return YES; 
    } 

    return NO; 
} 

回答

0

尝试向平移处理代码添加一些附加约束。也许是这样的:

if (oldCenter.x + tranlation.x >= superView.frame.size.width - self.circleRadius 
|| oldCenter.x + tranlation.x <= self.circleRadius) 
{ 
    newCenter.x = oldCenter.x; 
} 
else 
{ 
    newCenter.x = oldCenter.x + tranlation.x; 
} 

这是一个非常简单的例子,希望它有帮助。