如何添加多个不同形状的imageView?

时间:2013-02-26 12:05:53

标签: iphone ios uiimageview uibezierpath

目前我正在为拼贴应用程序工作。我想绘制拼贴画框,然后我需要添加相机胶卷或相机的图像。我需要以下类型视图 enter image description here

我添加了5个UIImageViews。为了绘制形状,我添加了类似于imageview1的UIBezierPath

UIBezierPath *path1 = [[UIBezierPath alloc] init];

        [path1 moveToPoint:CGPointMake(0, 0)];
        [path1 addLineToPoint:CGPointMake(150, 0)];
        [path1 addLineToPoint:CGPointMake(0, 150)];
        [path1 addLineToPoint:CGPointMake(0, 0)];

 UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 0, 160, 300)];
imgView1 . tag = 2;
imgView1 . userInteractionEnabled = YES;
imgView1. backgroundColor = [UIColor greenColor];
CGPathRef borderPathRef2 = [path1 CGPath];
CAShapeLayer *borderShapeLayer2 = [[CAShapeLayer alloc] init];
[borderShapeLayer2 setPath:borderPathRef2];
[[imgView1 layer] setMask:borderShapeLayer2];
imgView1.layer.masksToBounds = YES;
[borderShapeLayer2 release];

[view1 addSubview:imgView1];

像这样我已经为所有人做了5.但是在添加了imageView5后,在所有其他4个视图上都没有检测到触摸,因为它的帧重叠在另外四个imageview上。

所以我没有得到如何设计这个。我需要通过触摸操作将图像添加到所有图像视图中。

请帮帮我。如果有人对此有任何疑问,请分享。

提前致谢。

以下是来自 Insta Collage app 的示例2。 enter image description here

3 个答案:

答案 0 :(得分:3)

我通过覆盖UTiew的hitTest:withEvent:方法解决了这个问题。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    //Getting View which is touched.
    UIView *testView = [super hitTest:point withEvent:event];

    //If this is self. and point hitted on Masked Layer(Which is hiding our UIView).
    if(testView == self && testView.layer.mask != nil && CGPathContainsPoint([(CAShapedLayer*)testView.layer.mask path], NULL, point, false))
    {
            //Then return nil. So Touch thinks that UIView has not clicked. and touch is passed to UIView which is behind this view. And again hitTest is performed on inner UIViews.
            testView = nil;
    }

    //Return nil. So touch thinks that UIView is not clicked.
    //Return self. So touch thinks self is clicked. and no more hitTest is performed.
    return testView;
}

现在我为这个问题实现了一个名为IQIrregularView的控件。 检查一下。

https://github.com/hackiftekhar/IQIrregularView

答案 1 :(得分:0)

你应该尝试随机形状的按钮

您可以在GitHub

上找到的此自定义类中找到您的解决方案

它对我有用,希望对你也有用......

快乐编码...........

答案 2 :(得分:0)

我还试图使用Swift来检测UIBezierPath路径内UIView上的触摸

我的情况是:

- UIView (Container)
  - UIImageView (masked with Triangle UIBezierPath)
  - UIImageView (masked with Triangle UIBezierPath)

在我的容器中,我覆盖hitTest并返回被触摸的子视图,如下所示:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {        
    for subview in subviews.reversed() {

        // ** convert the touch to the subview space:
        let convertedPoint = subview.convert(point, from: self)

        // ** if the subview layer mask exists I know that it has a UIBezierPath:
        if let layerShape = subview.layer.mask as? CAShapeLayer {
            if let shapePath = layerShape.path, shapePath.contains(convertedPoint) {
                // ** return the subview with the path that contains the converted point
                return subview
            }
        }
    }

    return nil
}

希望有帮助!