裁剪UIImageView和手势识别器

时间:2015-05-14 01:44:08

标签: ios iphone uiview uiimageview uiimage

我有ImageView,我用它来裁剪:

self.contentMode = UIViewContentModeScaleAspectFill;
self.layer.cornerRadius = self.bounds.size.height / 2.0;
self.layer.masksToBounds = YES;

然后我添加了手势识别器,但它在裁剪区域中闪现。

如何避免在裁剪区域射击?

2 个答案:

答案 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)

  1. 将您的班级设为符合UIGestureRecognizerDelegate
  2. 将您的手势委托设置为自我
  3. 然后使用此委托来决定是否要点火

    -(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

  4. 示例代码

    -(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;}