gpt4 book ai didi

ios - 如何快速制作不同形状的 ImageView

转载 作者:行者123 更新时间:2023-11-28 10:22:20 25 4
gpt4 key购买 nike

enter image description here

我想将普通的 ios imageview 更改为下面的图像形状(如弧形)

最佳答案

您可以使用它来根据您的要求设计形状,您可以向路径添加额外的线条以防您需要修改贝塞尔曲线路径。创建一个自定义 UIImageView 类并将 Storyboard中的 ImageView 子类化到您的自定义类。

import UIKit

class customImageView: UIImageView {


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
// override func drawRect(rect: CGRect)
// {
//
//
// }


override func setNeedsLayout() {
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: self.frame.size.width/2, y: self.frame.size.height))
path.addLineToPoint(CGPoint(x: self.frame.size.width, y: self.frame.size.height/2))
path.addLineToPoint(CGPoint(x: self.frame.size.width/2, y: 0))
path.addArcWithCenter(CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2), radius: self.frame.size.width/2, startAngle:-CGFloat(M_PI_2), endAngle: CGFloat(M_PI_2), clockwise: false)

path.moveToPoint(CGPoint(x: self.frame.size.width/2, y: self.frame.size.height))
path.closePath()
UIColor.redColor().setFill()
path.stroke()
path.bezierPathByReversingPath()
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = path.CGPath
shapeLayer.fillColor = UIColor.redColor().CGColor
self.layer.mask = shapeLayer;
self.layer.masksToBounds = true;
}

}

如果你想通过代码添加图片,请像这样使用它

 func shapeImage()
{
let customImgView = customImageView()
customImgView.image = UIImage(named: "Image")
customImgView.frame = CGRectMake(0, 0, 250, 250)

self.view.addSubview(customImgView)
}

左边是实际图片,右边是在模拟器中按照你的要求整形后的图片

enter image description here

关于ios - 如何快速制作不同形状的 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33163317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com