uipangeSturerognizer和随机颜色
我有panperformed()
和RandomColors()
方法。当我将手指拖到屏幕上时,视图的颜色变化很快。
使用Pan手势时,如何使背景颜色更改慢3倍?
func randomColors() -> UIColor {
let r = CGFloat(arc4random_uniform(255)) / 255.0
let g = CGFloat(arc4random_uniform(255)) / 255.0
let b = CGFloat(arc4random_uniform(255)) / 255.0
return UIColor(red: r, green: g, blue: b, alpha: 1.0)
}
@IBAction func panPerformed(_ sender: UIPanGestureRecognizer) {
if sender.state == .began || sender.state == .changed {
let changeX = (self.view?.center.x)!
let changeY = (self.view?.center.y)!
sender.view?.center = CGPoint(x: changeX, y: changeY)
sender.setTranslation(CGPoint.zero, in: self.view)
self.view.backgroundColor = self.randomColors()
}
}
我在语法(感谢@duncan c)方面添加了此代码:
// Add some instance variables:
var r: Int = 0
var g: Int = 0
var b: Int = 0
override func viewDidLoad() {
// Give your background view an initial color
r = Int.random(in: 0...255)
g = Int.random(in: 0...255)
b = Int.random(in: 0...255)
self.view.backgroundColor = currentColor()
// Convert the r/g/b Int values to a UIColor
func currentColor() -> UIColor {
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
}
// Randomly change the input value by +/- 5
func addOrSubtract(from: Int) -> Int {
var result = from + Int.random(in: -5...5)
// Don't let the new value go < 0 or > 255
if result < 0 {
result = 0
} else if result > 255 {
result = 255
}
return Int()
}
// Adjust the r, g, and b values up or down a little.
func randomizeColor() {
r = addOrSubtract(from: r)
g = addOrSubtract(from: g)
b = addOrSubtract(from: b)
}
@IBAction func panPerformed(_ sender: UIPanGestureRecognizer) {
if sender.state == .began || sender.state == .changed {
let changeX = (self.view?.center.x)!
let changeY = (self.view?.center.y)!
sender.view?.center = CGPoint (x: changeX, y: changeY)
sender.setTranslation(CGPoint.zero, in: self.view)
if changeX + changeY > 5 {
randomizeColor()
// self.view.backgroundColor = self.randomColours() - the previous code
self.view.backgroundColor = currentColor()
}
}
}
}
当我将手指拖到屏幕视图的颜色上时,将手指拖到黑色而不改变颜色时。什么原因? addorSubtract()?
I have panPerformed()
and randomColors()
methods. When I drag my finger on the screen, view's color is changing very fast.
How to make background color change 3 times slower when using Pan Gesture?
func randomColors() -> UIColor {
let r = CGFloat(arc4random_uniform(255)) / 255.0
let g = CGFloat(arc4random_uniform(255)) / 255.0
let b = CGFloat(arc4random_uniform(255)) / 255.0
return UIColor(red: r, green: g, blue: b, alpha: 1.0)
}
@IBAction func panPerformed(_ sender: UIPanGestureRecognizer) {
if sender.state == .began || sender.state == .changed {
let changeX = (self.view?.center.x)!
let changeY = (self.view?.center.y)!
sender.view?.center = CGPoint(x: changeX, y: changeY)
sender.setTranslation(CGPoint.zero, in: self.view)
self.view.backgroundColor = self.randomColors()
}
}
I add this code with minor corrections in terms of syntax (thanks @Duncan C):
// Add some instance variables:
var r: Int = 0
var g: Int = 0
var b: Int = 0
override func viewDidLoad() {
// Give your background view an initial color
r = Int.random(in: 0...255)
g = Int.random(in: 0...255)
b = Int.random(in: 0...255)
self.view.backgroundColor = currentColor()
// Convert the r/g/b Int values to a UIColor
func currentColor() -> UIColor {
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
}
// Randomly change the input value by +/- 5
func addOrSubtract(from: Int) -> Int {
var result = from + Int.random(in: -5...5)
// Don't let the new value go < 0 or > 255
if result < 0 {
result = 0
} else if result > 255 {
result = 255
}
return Int()
}
// Adjust the r, g, and b values up or down a little.
func randomizeColor() {
r = addOrSubtract(from: r)
g = addOrSubtract(from: g)
b = addOrSubtract(from: b)
}
@IBAction func panPerformed(_ sender: UIPanGestureRecognizer) {
if sender.state == .began || sender.state == .changed {
let changeX = (self.view?.center.x)!
let changeY = (self.view?.center.y)!
sender.view?.center = CGPoint (x: changeX, y: changeY)
sender.setTranslation(CGPoint.zero, in: self.view)
if changeX + changeY > 5 {
randomizeColor()
// self.view.backgroundColor = self.randomColours() - the previous code
self.view.backgroundColor = currentColor()
}
}
}
}
When I drag my finger on screen view's color turn on black without changing colours. What is the reason? addOrSubtract()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在编制自己的功能。这实际上没有一种“正确的方式”,因为这是您的创建。
如果您不希望颜色突然变化,为什么不保存先前的颜色,并每隔几个拖动每几个颜色通道添加非常小的随机更改呢?类似的事情:(
将上述视为伪代码。它可能有一些语法错误,甚至可能是错误的。它并不是要复制/粘贴。)
You are making up your own feature. There isn't really a "right way" since this is your creation.
If you don't want the colors to change suddenly, why not save the previous color, and add a very small random change to each color channel every few points of finger dragging? Something like this:
(Think of the above as pseudo-code. It probably has a few syntax errors and might even be buggy. It's not meant to be copy/paste ready.)