uipangeSturerognizer和随机颜色

发布于 2025-02-13 08:19:34 字数 2610 浏览 2 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2025-02-20 08:19:34

您正在编制自己的功能。这实际上没有一种“正确的方式”,因为这是您的创建。

如果您不希望颜色突然变化,为什么不保存先前的颜色,并每隔几个拖动每几个颜色通道添加非常小的随机更改呢?类似的事情:(


// Add some instance variables:
var r: Int = 0
var g: Int = 0
var b: Int = 0

func viewDidLoad() {
  // Give your background view an initial color
  r = Int.random(0...255)
  g = Int.random(0...255)
  b = Int.random(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(-5...5)

    // Don't let the new value go < 0 or > 255
    if result < 0 {
        result = 0
    } else if result > 255 {
        result = 255
    }
}  

// Adjust the r, g, and b values up or down a little.
func randomizeColor() {
    r = addOrSubtract(from: r)
    g = addOrSubtract5(from: g)
    b = addOrSubtract5(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 the user move their finger by more than a few points, 
        // change the background color slightly
        if changeX + changeY > 5 {
            randomizeColor()
            self.view.backgroundColor = currentColor() 
        }
    }
}

将上述视为伪代码。它可能有一些语法错误,甚至可能是错误的。它并不是要复制/粘贴。)

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:


// Add some instance variables:
var r: Int = 0
var g: Int = 0
var b: Int = 0

func viewDidLoad() {
  // Give your background view an initial color
  r = Int.random(0...255)
  g = Int.random(0...255)
  b = Int.random(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(-5...5)

    // Don't let the new value go < 0 or > 255
    if result < 0 {
        result = 0
    } else if result > 255 {
        result = 255
    }
}  

// Adjust the r, g, and b values up or down a little.
func randomizeColor() {
    r = addOrSubtract(from: r)
    g = addOrSubtract5(from: g)
    b = addOrSubtract5(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 the user move their finger by more than a few points, 
        // change the background color slightly
        if changeX + changeY > 5 {
            randomizeColor()
            self.view.backgroundColor = currentColor() 
        }
    }
}

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文