How to make imageview with different shape in swift(如何快速制作不同形状的图像视图)
问题描述
我想将普通的 ios imageview 更改为下面的图像形状(如弧)
I want to change the normal ios imageview to this below image shape(Like arc)
推荐答案
您可以使用它来根据您的要求设计形状,您可以在路径中添加额外的线条以防您需要修改贝塞尔路径.创建一个自定义 UIImageView 类并将情节提要中的图像视图子类化为您的自定义类.
You can use this to design the shape as per your requirement, you can add extra lines to the path incase you need to modify the bezier path. Create a custom UIImageView class and subclass the image view in storyboard to your custom class.
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;
}
}
如果您想通过代码添加图像,请像这样使用它
If you want to add the image through code kindly use it like this
func shapeImage()
{
let customImgView = customImageView()
customImgView.image = UIImage(named: "Image")
customImgView.frame = CGRectMake(0, 0, 250, 250)
self.view.addSubview(customImgView)
}
这是左侧的实际图像,右侧是根据您的要求在模拟器中整形后的图像
This is the actual image on left hand side and the image after shaping as per your requirement in the simulator on the right hand side
这篇关于如何快速制作不同形状的图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何快速制作不同形状的图像视图


基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01