在随机位置画圆

时间:2018-11-23 11:19:16

标签: ios swift

问题如下: 1.有必要在屏幕上随机放置圆圈。 2.圆必须具有不同的半径,并且彼此不能重叠且不能超出屏幕。

为View创建了一个单独的文件。这是代码:

override func draw(_ rect: CGRect) {

    var number = 1

    while number <= 5 {
        //1. Screen center coordinates
        let viewMidX = self.bounds.size.height
        let viewMidY = self.bounds.size.width

        //2. Circle radius
        let circleWidth = CGFloat(20 + arc4random_uniform(30))
        let circleHeight = circleWidth

        //3. Two random number
        let randomNumberOne = CGFloat(arc4random_uniform(40))
        let randomNumberTwo = CGFloat(arc4random_uniform(20))

        //4. Coordinates and sizes of the circle
        let rect = CGRect(x: viewMidX - randomNumberOne , y: viewMidY - randomNumberTwo , width: circleWidth, height: circleHeight)

        //5. Draw circle
        let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: circleWidth / 2)

        //6. Circle color
        myColor.setFill()
        circlePath.fill()


        myColor.setStroke()
        circlePath.stroke()

        number += 1
    }

在ViewController文件中,我为此圆做一些动画(脉动)

//7.
    self.view.transform = CGAffineTransform(scaleX: 1, y: 1)

    //8. Animation
    UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
        self.view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
    }) { finished in
    }

当我开始时,我画了一个圆圈,仅此而已。

enter image description here

2 个答案:

答案 0 :(得分:0)

马上有一些明显的错误。

let viewMidX = self.bounds.size.height

应该是

let viewMidX = self.bounds.size.width / 2

viewMidY做类似的事情

接下来查看设置随机数的距离,移动最小。 (40和20) 下面的图片表示应该在哪里绘制圆。

enter image description here

所以我推荐的是更多类似的东西。

import UIKit

class ViewController: UIViewController {

    let circleView = CircleView()
    let tapView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(circleView)
        view.addSubview(tapView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        circleView.frame = view.frame
        tapView.frame = view.frame
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        animate()
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        circleView.setNeedsDisplay()
    }

    // Animation
    func animate() {
        UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
            self.circleView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        }, completion: nil)
    }

}

class CircleView: UIView {

    var myColor = UIColor.red
    var borderColor = UIColor.black
    var usedRects: [CGRect] = []
    var usedDiameters: [CGFloat] = []

    init() {
        super.init(frame: .zero)
        backgroundColor = .lightGray
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        setNeedsDisplay()
    }

    override func draw(_ rect: CGRect) {
        usedRects = []
        usedDiameters = []
        for _ in 0..<5 {

            //1. Circle Diameter
            let diameter = getDiameter()

            //2. Circle Rect
            let rect = getRect(diameter: diameter)

            //3. Circle Path
            let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: diameter / 2)

            //4. Circle Color
            myColor.setFill()

            //5. Draw Circle
            circlePath.fill()

            //6. Circle Border Color
            borderColor.setStroke()

            //7. Draw Circle Boder
            circlePath.stroke()
        }
    }

    func getDiameter() -> CGFloat {
        let diameter = CGFloat(20 + arc4random_uniform(30))
        for usedDiameter in usedDiameters
            where usedDiameter == diameter {
                return getDiameter()
        }
        usedDiameters.append(diameter)
        return diameter
    }

    func getRect(diameter: CGFloat) -> CGRect {
        let randomWidth = CGFloat(arc4random_uniform(UInt32(self.bounds.size.width)))
        let randomHeight = CGFloat(arc4random_uniform(UInt32(self.bounds.size.height)))

        let rect = CGRect(x: randomWidth, y: randomHeight, width: diameter, height: diameter)

        for usedRect in usedRects
            where usedRect.intersects(rect) {
                return getRect(diameter: diameter)
        }
        usedRects.append(rect)
        return rect
    }

}

答案 1 :(得分:-1)

您也许可以像这样在mainViewController中而不是在CircleView的draw函数中添加addSubview(circle)

ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        i = 0
        while i < 5 {
            let circleView = CircleView()
            self.view.addSubView(circleView)
            i += 1
        }
    }
}

在自定义CircleView的函数绘制中,不要绘制圆弧,而只需使用随机坐标绘制视图即可。