我有ImageView,我用它来裁剪:
self.contentMode = UIViewContentModeScaleAspectFill;
self.layer.cornerRadius = self.bounds.size.height / 2.0;
self.layer.masksToBounds = YES;
然后我添加了手势识别器,但它在裁剪区域中闪现。
如何避免在裁剪区域射击?
答案 0 :(得分:3)
使用CAShapeLayer可以更加通用且灵活地屏蔽图像。您可以创建任何形状(包括圆形)以用作蒙版。通过使用此方法裁剪图像视图而不是使用(select @prev := -1, @rc := 0) q
,您可以检查触摸点是否在图层的路径中(cornerRadius
)。在UIBezierPath
子类中添加以下代码以创建掩码,并在.h文件中创建属性,形状。
UIImageView
在控制器中,添加点击手势识别器,并在其操作方法中使用此代码
self.shape = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = self.shape.CGPath;
self.layer.mask = shapeLayer;
答案 1 :(得分:0)
UIGestureRecognizerDelegate
然后使用此委托来决定是否要点火
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
示例代码
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint touchPoint = [touch locationInView:self.imageview];
if (CGRectContainsPoint(self.imageview.bounds, touchPoint)) {
CGFloat centerX = CGRectGetMidX(self.imageview.bounds);
CGFloat centerY = CGRectGetMidY(self.imageview.bounds);
CGFloat radius2 = pow((touchPoint.x -centerX),2)+ pow((touchPoint.y - centerY), 2);
if (radius2 < pow(CGRectGetWidth(self.imageview.frame)/2, 2)) {
return YES;
}
}
return NO;}