答案 0 :(得分:4)
您可以根据需要使用此设计形状,您可以在路径中添加额外的行,以修改贝塞尔曲线路径。创建一个自定义UIImageView类,并将storyboard中的图像视图子类化为您的自定义类。
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)
}
这是左侧的实际图像和根据您在右侧模拟器中的要求进行整形后的图像