Ios 将形状的移动限制为UIImageView的尺寸
使用我遇到的一些代码在图像上绘制一个圆形。平移和挤压手势用于正在绘制的圆形状。形状的移动跨越UIImageView的维度,到达未显示图像的UI的其余部分。我尝试合并其他示例中使用的方法,其中需要平移和收缩限制,以便将圆形状的移动限制在UIImageView的边界内,但无法使其中任何一个正常工作。我知道我需要控制平移和收缩的中心和半径值,以便它们不会超出UIImageView的边界,但对于如何执行这一操作,我没有丝毫线索。附件是我正在使用的代码。谢谢你的帮助Ios 将形状的移动限制为UIImageView的尺寸,ios,uibezierpath,uipangesturerecognizer,cashapelayer,uipinchgesturerecognizer,Ios,Uibezierpath,Uipangesturerecognizer,Cashapelayer,Uipinchgesturerecognizer,使用我遇到的一些代码在图像上绘制一个圆形。平移和挤压手势用于正在绘制的圆形状。形状的移动跨越UIImageView的维度,到达未显示图像的UI的其余部分。我尝试合并其他示例中使用的方法,其中需要平移和收缩限制,以便将圆形状的移动限制在UIImageView的边界内,但无法使其中任何一个正常工作。我知道我需要控制平移和收缩的中心和半径值,以便它们不会超出UIImageView的边界,但对于如何执行这一操作,我没有丝毫线索。附件是我正在使用的代码。谢谢你的帮助 -(void)setUpCropToo
-(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;
}
尝试向平移处理代码添加一些附加约束。可能是这样的:
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;
}
if(oldCenter.x+translation.x>=superView.frame.size.width-self.circleRadius
||oldCenter.x+translation.x