ios – 如何使用Swift以编程方式使用交替条纹图案填充UIView?

ios – 如何使用Swift以编程方式使用交替条纹图案填充UIView?,第1张

概述我可以使用带有UIColor的图像(pattern Image:UI Image(命名为:“pattern.png”))来实现交替条纹图案填充. 但是,如何通过一些简单的紧凑代码以编程方式实现交替条纹图案填充? 以下是我想要实现的两个例子. 题 Using code, how do I fill a UIView with different alternating color stripes u 我可以使用带有UIcolor的图像(pattern Image:UI Image(命名为:“pattern.png”))来实现交替条纹图案填充.

但是,如何通过一些简单的紧凑代码以编程方式实现交替条纹图案填充?

以下是我想要实现的两个例子.

Using code,how do I fill a UIVIEw with different alternating color stripes using Swift?

(1) a two color alternating pattern running top to bottom (90
degrees)?

(2) a three color alternating pattern running top left to bottom right
(45 degrees)?

解决方法 使用下面的代码可以实现具有条纹图案的视图填充,条纹图案具有可调节的条带宽度和旋转的两个,三个或更多个颜色条纹.此代码提供了下面示例图像中显示的三色条纹图案.

脚步:

Add the below code to VIEwController.swift Add a UIVIEw to Storyboard. Add new alignment contstraints to the UIVIEw on the Storyboard; Horizontally in Container = 0,Vertically in Container = 0. In the IDentity Inspector,set the UIVIEw custom class to ‘colorStripesVIEw’. Connect the UIVIEw on the Storyboard to the vIEwPattern IBOutlet in the code.

码:

////   VIEwController.swift    //// 1. Add the below code to VIEwController.swift    //// 2. Add a UIVIEw to Storyboard.    //// 3. Add new alignment contstraints to the UIVIEw on the Storyboard; Horizontally in Container = 0,Vertically in Container = 0.    //// 4. In the IDentity Inspector,set the UIVIEw custom class to 'colorStripesVIEw'.    //// 5. Connect the UIVIEw on the Storyboard to the vIEwPattern IBOutlet in the code.    import UIKit    class VIEwController: UIVIEwController {        @IBOutlet weak var vIEwPattern: UIVIEw!        overrIDe func vIEwDIDLoad() {            super.vIEwDIDLoad()            //// Extend wIDth and height constraints by factor of 2 for vIEwPattern rotation.            let wIDthConstraint = NSLayoutConstraint(item: vIEwPattern,attribute: .WIDth,relatedBy: .Equal,toItem: nil,attribute: .NotAnAttribute,multiplIEr: 1.0,constant: (max(vIEw.bounds.height,vIEw.bounds.wIDth)*2))            vIEwPattern.addConstraint(wIDthConstraint)            let heightConstraint = NSLayoutConstraint(item: vIEwPattern,attribute: .Height,vIEw.bounds.wIDth)*2))            vIEwPattern.addConstraint(heightConstraint)            //// Rotate pattern 0 degrees - vertical.            //vIEwPattern.transform = CGAffinetransformMakeRotation(CGfloat(M_PI*0/180))            //// Rotate pattern 45 degrees - diagonal top right to bottom left.            //vIEwPattern.transform = CGAffinetransformMakeRotation(CGfloat(M_PI*45/180))            //// Rotate pattern 90 degrees - horizontal.            //vIEwPattern.transform = CGAffinetransformMakeRotation(CGfloat(M_PI*90/180))            //// Rotate pattern 135 degrees - diagonal top left to bottom right.            vIEwPattern.transform = CGAffinetransformMakeRotation(CGfloat(M_PI*135/180))            //// Set vIEw color            vIEwPattern.backgroundcolor = UIcolor.clearcolor()        }        overrIDe func dIDReceiveMemoryWarning() {            super.dIDReceiveMemoryWarning()        }    }    class colorStripesVIEw: UIVIEw {        overrIDe func drawRect(rect: CGRect) {            //// Set pattern tile colors wIDth and height; adjust the color wIDth to adjust pattern.            let color1 = UIcolor(red: 255/255,green: 255/255,blue: 10/255,Alpha: 1.0)            let color1WIDth: CGfloat = 10            let color1Height: CGfloat = 10            let color2 = UIcolor(red: 0/255,green: 0/255,blue: 254/255,Alpha: 1.0)            let color2WIDth: CGfloat = 10            let color2Height: CGfloat = 10            let color3 = UIcolor(red: 0/255,green: 128/255,blue: 128/255,Alpha: 1.0)            let color3WIDth: CGfloat = 10            let color3Height: CGfloat = 10            //// Set pattern tile orIEntation vertical.            let patternWIDth: CGfloat = (color1WIDth + color2WIDth + color3WIDth)            let patternHeight: CGfloat = min(color1Height,color2Height,color3Height)            //// Set pattern tile size.            let patternSize = CGSize(wIDth: patternWIDth,height: patternHeight)            //// Draw pattern tile            let context = UIGraphicsGetCurrentContext()            UIGraphicsBeginImageContextWithOptions(patternSize,false,0.0)            let color1Path = UIBezIErPath(rect: CGRect(x: 0,y: 0,wIDth: color1WIDth,height: color1Height))            color1.setFill()            color1Path.fill()            let color2Path = UIBezIErPath(rect: CGRect(x: color1WIDth,wIDth: color2WIDth,height: color2Height))            color2.setFill()            color2Path.fill()            let color3Path = UIBezIErPath(rect: CGRect(x: color1WIDth + color2WIDth,wIDth: color3WIDth,height: color3Height))            color3.setFill()            color3Path.fill()            let image = UIGraphicsGetimageFromCurrentimageContext()            UIGraphicsEndImageContext()            //// Draw pattern in vIEw            UIcolor(patternImage: image).setFill()            CGContextFillRect(context,rect)        }    }

模拟器:

总结

以上是内存溢出为你收集整理的ios – 如何使用Swift以编程方式使用交替条纹图案填充UIView?全部内容,希望文章能够帮你解决ios – 如何使用Swift以编程方式使用交替条纹图案填充UIView?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072993.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存