限制形状移动到UIImageView的尺寸

时间:2015-06-17 08:34:27

标签: ios uibezierpath uipangesturerecognizer cashapelayer uipinchgesturerecognizer

使用我遇到的一些代码在图像上绘制圆形。平移和捏合手势用于绘制的圆形。形状的移动跨越UIImageView的尺寸,移动到未显示图像的UI的其余部分。我试图结合其他示例中使用的方法,其中需要平移和捏合限制以限制圆形的移动到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;
}

1 个答案:

答案 0 :(得分: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;
}

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