1
A
回答
2
3
这是一个基于代码的解决方案,不需要任何图像。这将使用UIBezierPath
创建自定义视图来创建正弦波。
import UIKit
class WavyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
return nil // TODO
}
override func draw(_ rect: CGRect) {
// Fill the whole background with the darkest blue color
UIColor(red: 0.329, green: 0.718, blue: 0.875, alpha: 1).set()
let bg = UIBezierPath(rect: rect)
bg.fill()
// Add the first sine wave filled with a very transparent white
let top1: CGFloat = 17.0
let wave1 = wavyPath(rect: CGRect(x: 0, y: top1, width: frame.width, height: frame.height - top1), periods: 1.5, amplitude: 21, start: 0.55)
UIColor(white: 1.0, alpha: 0.1).set()
wave1.fill()
// Add the second sine wave over the first
let top2: CGFloat = 34.0
let wave2 = wavyPath(rect: CGRect(x: 0, y: top2, width: frame.width, height: frame.height - top2), periods: 1.5, amplitude: 21, start: 0.9)
UIColor(white: 1.0, alpha: 0.15).set()
wave2.fill()
// Add the text
let paraAttrs = NSMutableParagraphStyle()
paraAttrs.alignment = .center
let textRect = CGRect(x: 0, y: frame.maxY - 64, width: frame.width, height: 24)
let textAttrs = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20), NSForegroundColorAttributeName: UIColor(white: 1.0, alpha: 0.9), NSParagraphStyleAttributeName: paraAttrs]
("New user? Register here." as NSString).draw(in: textRect, withAttributes: textAttrs)
}
// This creates the desired sine wave bezier path
// rect is the area to fill with the sine wave
// periods is how may sine waves fit across the width of the frame
// amplitude is the height in points of the sine wave
// start is an offset in wavelengths for the left side of the sine wave
func wavyPath(rect: CGRect, periods: Double, amplitude: Double, start: Double) -> UIBezierPath {
let path = UIBezierPath()
// start in the bottom left corner
path.move(to: CGPoint(x: rect.minX, y: rect.maxY))
let radsPerPoint = Double(rect.width)/periods/2.0/Double.pi
let radOffset = start * 2 * Double.pi
let xOffset = Double(rect.minX)
let yOffset = Double(rect.minY) + amplitude
// This loops through the width of the frame and calculates and draws each point along the size wave
// Adjust the "by" value as needed. A smaller value gives smoother curve but takes longer to draw. A larger value is quicker but gives a rougher curve.
for x in stride(from: 0, to: Double(rect.width), by: 6) {
let rad = Double(x)/radsPerPoint + radOffset
let y = sin(rad) * amplitude
path.addLine(to: CGPoint(x: x + xOffset, y: y + yOffset))
}
// Add the last point on the sine wave at the right edge
let rad = Double(rect.width)/radsPerPoint + radOffset
let y = sin(rad) * amplitude
path.addLine(to: CGPoint(x: Double(rect.maxX), y: y + yOffset))
// Add line from the end of the sine wave to the bottom right corner
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
// Close the path
path.close()
return path
}
}
// This creates the view with the same size as the image posted in the question
let wavy = WavyView(frame: CGRect(x: 0, y: 0, width: 502, height: 172))
在操场斯威夫特运行代码,结果给出如下:
很明显,你可以在代码调整的任何值以上调整的结果。
+0
伟大的方法! –
相关问题
- 1. WPF边界形状
- 2. 如何更改UIView的形状
- 3. 定制边界矩形的形状
- 4. 查找平面图(几何形状)的边界(边界)
- 5. 查找GDI +形状的边界
- 6. 更改UIView中元素的大小,并使用[[UIScreen mainScreen]边界]
- 7. 一旦调用UIPinchGestureRecognizer就更改UIView的边界
- 8. 更改窗口的边界
- 9. 如何在android中更改自定义形状的边界颜色?
- 10. 在OpenGLes 1.1中设置边界形状?
- 11. 边界形状识别忽略缩放
- 12. 更改/添加空间边界/多边形shapefile R
- 13. 更多精确形状的边界位置
- 14. 为什么在UIView中有框架矩形和边界矩形?
- 15. 如何更改CAShapeLayer中的多边形形状?
- 16. 多边形的边界框
- 17. 更改.Net数组边界
- 18. iPhone自定义UIView边界
- 19. Matlab - 将点正交传播到形状边界的边缘
- 20. 旋转的UIView和核心图形的边界
- 21. 用于项目交互的Qt边界矩形/形状
- 22. 通过坐标计算2D形状的最小边界矩形
- 23. 更改阵列的形状
- 24. 更改jpeg的形状
- 25. 更改appWidget中的形状
- 26. 更改JButton的形状
- 27. Android的 - 更改密度边界的EditText
- 28. 更改imageView的UITableViewCell的边界
- 29. 如何更改UIView顶部边框
- 30. 更改边界框的大小
使用imageView是一个选项,但有一些缺点。 a)添加大量的图像会增加您的应用程序大小b)您需要根据设备大小维护多个版本的图像(例如:运行您需要单独的图像为ipad vs iphone。)我会看看使用UIView创建此和绘制形状。 – TheAppMentor