【问题标题】:How to add multiple imageView with different shapes?如何添加多个不同形状的imageView?
【发布时间】:2023-08-15 01:13:02
【问题描述】:

目前我正在为一个拼贴应用工作。我想画拼贴画框,然后我需要从相机胶卷或相机中添加图像。我需要以下类型视图

我添加了 5 个 UIImageViews。为了绘制形状,我添加了 UIBezierPath,例如 imageview1

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 应用程序的示例 2。

【问题讨论】:

  • 你为什么不使用按钮。你有背景图片以及动作?
  • 如果我要使用按钮,那么只有 imageview5 会执行按钮操作。
  • 您能分享一下您想要实现的确切图像设计吗?该框架显示了实现相同目标的几种替代方法。为了具体到您的需要,张贴链接
  • 如果你只需要恢复用户交互,在你添加其他imageviews之后,添加这个[self.view bringSubviewToFront:imageView5];,然后你会得到它
  • 这取决于用户。如果用户想先在 imageview1 上添加图像,或者可能是 imageview 5。就是这种情况。

标签: iphone ios uiimageview uibezierpath


【解决方案1】:

我已经通过覆盖 UIView 的 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

【讨论】:

    【解决方案2】:

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

    您可以在GitHub 上的这个自定义课程中找到您的解决方案

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

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

    【讨论】:

    • 可能有机会点击OBShapedButton。如果您尝试我的回答,它将适用于所有条件。
    【解决方案3】:

    我还尝试使用 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
    }
    

    希望有帮助!

    【讨论】:

      最近更新 更多