如何创建非动画的曲线UIView/UIImageView?

4
我应该如何创建一个UIView,使其看起来像这样,并且它的子视图也有相同的效果。 在网络上搜索后,我发现可以通过CALayer实现,但我对此不够了解。请帮助我,谢谢。

1
我认为你不能使用Quartz或UIKit。当然,你可以使用贝塞尔路径构建曲线路径,但它们仅适用于已经绘制的内容,而不是子视图或子层。 使用Quartz和CATransform3D,您可以实现某种形式的三维透视视图。<br> 你想做的是OpenGL的职责。 - Andrea
没有OpenGL是不可能的吗? - BHASKAR
使用带有一些透明度的图像视图会出现什么问题? - Marcus Adams
2个回答

0

根据我的水平和理解,我做了一些编码,可能对你有所帮助,如果不需要请忽略。

第一步是从新文件创建自定义视图。

接下来,我们需要将ViewController的view属性的类从UIView更改为我们的CustomView类。打开MainStoryboard.storyboard,在文档大纲中单击View图标。然后,打开identity inspector(选项+命令+3),并将类名更改为CustomView。

然后在自定义视图中添加此代码。

- (void)drawRect:(CGRect)rect
{
  // Drawing code
  UIImage *imageRecipe =[UIImage imageNamed:@"statement_card_1.png"];
  UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0 ,0 ,290, 200)];
  imgView.image = imageRecipe;
  [self addSubview:imgView];

  CGPoint secondPoint = CGPointZero;
  CGPoint point1 = CGPointZero;
  CGPoint initial = CGPointZero;

  UIBezierPath *aPath = [UIBezierPath bezierPath];

  initial = CGPointMake(30, 60);
  [aPath moveToPoint:initial];

  CGPoint point = CGPointMake(290, 60);
  CGPoint centerPoint = CGPointMake(point.x/2, -10);

  [aPath addQuadCurveToPoint:point controlPoint:centerPoint];


  secondPoint = CGPathGetCurrentPoint(aPath.CGPath);

  [aPath addLineToPoint:CGPointMake(secondPoint.x, 200.0)];

  point1 = CGPathGetCurrentPoint(aPath.CGPath);

  CGPoint p = CGPointMake(initial.x,point1.y);
  CGPoint cp = CGPointMake(point1.x/2 ,point1.y/2 + 30);

  [aPath addQuadCurveToPoint:p controlPoint:cp];

 [aPath addLineToPoint:CGPointMake(initial.x, secondPoint.y)];
 [aPath closePath];

 [aPath stroke];

 CAShapeLayer *maskLayer = [CAShapeLayer layer];
 maskLayer.frame = imgView.bounds;
 maskLayer.path = aPath.CGPath;
 imgView.layer.mask = maskLayer;
}

不,它没有工作,只需在ImageView上绘制曲线并进行遮罩。谢谢回复。 - BHASKAR

0

没有OpenGL或其他引擎是不可能实现的。你不能以这种方式变形CALayer。图层的基本思想是它是一个平面。

也许可以用图像和一些旋转的视图之类的东西来伪造它?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接