中和软件技术培训

上海中和软件公司培训专用博客

UIView绘画代码

UIView绘画代码


//
//  CIrcleView.swift
//  Canvas
//
//  Created by Hu on 2017/9/22.
//  Copyright © 2017年 Hu. All rights reserved.
//

import UIKit
@IBDesignable
class CIrcleView: UIView {
    @IBInspectable
    var Radius : CGFloat = 15
    {
        didSet{
            //刷新UIView
            setNeedsDisplay()
        }
    }
    @IBInspectable
    var PenColor : UIColor = .red {
        didSet{
            //刷新UIView
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var FillPenColor : UIColor = .green {
        didSet{
            //刷新UIView
            setNeedsDisplay()
        }
    }
    
    init(){
        super.init(frame: CGRect.zero)
    }
    required override init(frame: CGRect)
    {
        super.init(frame:frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        let x = UIPinchGestureRecognizer(target: self, action: #selector(scale))
        self.addGestureRecognizer(x)
    }
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        let ViewCenter = convert(center, from: superview)
        let path = UIBezierPath(arcCenter: ViewCenter, radius: Radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
        PenColor.setStroke()
        FillPenColor.setFill()
        path.stroke()
        path.fill()
    }
    @objc func scale(gesture : UIPinchGestureRecognizer){
         if (gesture.state == .changed){
            Radius *= gesture.scale
            gesture.scale = 1
        }
    }

}

博文最后更新时间:


评论

  • 四娘

    draw 函数里,CGFloat(2 * Double.pi) 直接可以简化为 2 * .pi。另外,Swift 里变量命名一般用小写字母开头的驼峰

发表评论

博客统计

访问量:26903

博文总数:16 评论总数:50

原创15 翻译0 转载1