colorArr.append の代わりに red, green, blue の値を print してみましょう。
swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view. 8 9 var colorArr:[UIColor] = [] 10 11 var red:CGFloat = 1 12 var green:CGFloat = 0 13 var blue:CGFloat = 0 14 15 for _ in 0 ..< 100 { 16 if green <= 1 && red == 1 && blue == 0{//赤1緑1青0 17 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 18 green += 0.06 19 }else if red >= 0 && blue == 0 {//赤0緑1青0 20 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 21 red -= 0.06 22 23 }else if blue < 1 && green > 1 {//赤0緑1青1 24 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 25 blue += 0.06 26 print(red,green,blue) 27 }else if green >= 0 && blue > 1{//赤0緑0青1 28 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 29 green -= 0.06 30 }else if red < 1{//赤1緑0青1 31 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 32 red += 0.06 33 }else if blue > 0 {//赤1緑0青0 34 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 35 blue -= 0.06 36 37 } 38 } 39 let radius = self.view.bounds.width / 4 40 let center = self.view.center 41 42 let theta = 2 * CGFloat.pi / CGFloat(colorArr.count) 43 44 for i in 0 ..< colorArr.count{ 45 let colorView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 46 colorView.backgroundColor = colorArr[i] 47 colorView.center.x = center.x + radius * cos(CGFloat(i) * theta) 48 colorView.center.y = center.y + radius * sin(CGFloat(i) * theta) 49 view.addSubview(colorView) 50 } 51 } 52} 53
swift
1for _ in 0 ..< 100 { 2 if green <= 1 && red == 1 && blue == 0{//赤1緑1青0 3 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 4 green += 0.06 5 }else if red >= 0 && blue == 0 {//赤0緑1青0 6 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 7 red -= 0.06 8 9 }else if blue < 1 && green > 1 {//赤0緑1青1 10 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 11 blue += 0.06 12 print(red,green,blue) 13 }else if green >= 0 && blue > 1{//赤0緑0青1 14 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 15 green -= 0.06 16 }else if red < 1{//赤1緑0青1 17 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 18 red += 0.06 19 }else if blue > 0 {//赤1緑0青0 20 colorArr.append(UIColor(red: red, green: green, blue: blue, alpha: 1)) 21 blue -= 0.06 22 23 }
丸い円でグラデーションを作るときに、この部分のif文のロジックがわかりません。どのようなフローになっているか教えてもらいたいです。
あなたの回答
tips
プレビュー