您好,登錄后才能下訂單哦!
前言
開發(fā)中總因?yàn)橐恍┍容^蛋疼的導(dǎo)角問題而困擾著我們,尤其是我們要給一個(gè)UIView導(dǎo)角成 左上 、左下。。。
這種需求很值得用代碼實(shí)現(xiàn)一下, 今天突然在AppCode找到了一篇好文章.于是有了下文
通常導(dǎo)角
self.view.cornerRadius = 20.0 self.view.clipToBounds = true
這兩行代碼是全方向?qū)Ы?/p>
如果像要搞成不同方向的話可以用iOS11 新的API和 iOS11以前的CAShapeLayer畫貝賽爾曲線來解決
首先我們要?jiǎng)?chuàng)建一個(gè)UIView
class ViewController: UIViewController { var cardView: UIView! override func viewDidLoad() { super.viewDidLoad() cardView = UIView() view.addSubview(cardView) cardView.translatesAutoresizingMaskIntoConstraints = false //把View居中 cardView.widthAnchor.constraint(equalToConstant: 200).isActive = true cardView.heightAnchor.constraint(equalToConstant: 200).isActive = true cardView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true cardView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true cardView.backgroundColor = UIColor(red: 1.0, green: 0.784, blue: 0.2, alpha: 1) } }
iOS11 以后蘋果提供了一個(gè)UIView的屬性叫maskedCorners用于CALayer的動(dòng)畫相關(guān).
public struct CACornerMask : OptionSet { public init(rawValue: UInt) public static var layerMinXMinYCorner: CACornerMask { get } public static var layerMaxXMinYCorner: CACornerMask { get } public static var layerMinXMaxYCorner: CACornerMask { get } public static var layerMaxXMaxYCorner: CACornerMask { get } }
下面我說一下
一般我們都為UIView寫個(gè) extension
extension UIView { func roundCorners(cornerRadius: Double) { self.layer.cornerRadius = CGFloat(cornerRadius) self.clipsToBounds = true if #available(iOS 11.0, *) { self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] } else { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) let maskLayer = CAShapeLayer() maskLayer.frame = self.bounds maskLayer.path = path.cgPath self.layer.mask = maskLayer } } }
這里區(qū)分了iOS11之前和之后的兩種搞法.
之前的話我們都是用一個(gè)貝塞爾曲線畫path.然后創(chuàng)建CAShapeLayer 給self.layer.mask做一種透明的遮罩來解決不同方向?qū)Ы菃栴}.
添加動(dòng)畫效果的導(dǎo)角
我們?cè)谠?code>ViewDidLoad()方法里面加個(gè)手勢(shì).
并寫好觸發(fā)的事件, 完整的代碼如下
import UIKit class ViewController: UIViewController { var cardView: UIView! override func viewDidLoad() { super.viewDidLoad() cardView = UIView() view.addSubview(cardView)cardView.translatesAutoresizingMaskIntoConstraints = false cardView.widthAnchor.constraint(equalToConstant: 200).isActive = true cardView.heightAnchor.constraint(equalToConstant: 200).isActive = true cardView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true cardView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true cardView.backgroundColor = UIColor(red: 1.0, green: 0.784, blue: 0.2, alpha: 1) let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(animateCornerChange(recognizer:))) cardView.addGestureRecognizer(tapRecognizer) } @objc func animateCornerChange(recognizer: UITapGestureRecognizer) { let targetRadius: Double = (cardView.layer.cornerRadius == 0.0) ? 100.0:0.0 if #available(iOS 10.0, *) { UIViewPropertyAnimator(duration: 0.4, curve: .easeInOut) { self.cardView.roundCorners(cornerRadius: targetRadius) }.startAnimation() } else { UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseInOut, animations: { }, completion: nil) } } } extension UIView { func roundCorners(cornerRadius: Double) { self.layer.cornerRadius = CGFloat(cornerRadius) self.clipsToBounds = true if #available(iOS 11.0, *) { self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] } else { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) let maskLayer = CAShapeLayer() maskLayer.frame = self.bounds maskLayer.path = path.cgPath self.layer.mask = maskLayer } } }
這里主要強(qiáng)調(diào)一下動(dòng)畫的新API
iOS10之后U增加一個(gè)新的動(dòng)畫效果API
UIViewPropertyAnimator(duration: 0.4, curve: .easeInOut) { //這里寫相關(guān)View的操作代碼 。。。eg:下面代碼 self.cardView.roundCorners(cornerRadius: targetRadius) }.startAnimation()
iOS之前可以通過古老的API來實(shí)現(xiàn)
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseInOut, animations: { //這里寫相關(guān)View的操作代碼 。。。eg:下面代碼 self.cardView.roundCorners(cornerRadius: targetRadius) }, completion: nil)
最終的效果
總結(jié)
iOS一些簡(jiǎn)單的動(dòng)畫導(dǎo)角比較常用所以記錄下來,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)億速云的支持。
本文Demo
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。