【问题标题】:Stop an animated square with button用按钮停止动画方块
【发布时间】:2014-12-03 19:14:15
【问题描述】:

我正在尝试使用按钮停止视图中的动画正方形(循环上下)并读取位置(x,y) 这是运动的代码

override func viewDidLoad() {
    super.viewDidLoad()

    while buttonPress == false {
    movement()
    }
}

func movement() {
    coloredSquare.backgroundColor = UIColor.blueColor()
    coloredSquare.frame = CGRect(x:0, y:500, width:50, height:50)
    self.view.addSubview(coloredSquare)
    movementDown()
}

func movementDown() {
    // lets set the duration to 1.0 seconds
    // and in the animations block change the background color
    // to red and the x-position  of the frame
    UIView.animateWithDuration(1.0, animations: {
        self.coloredSquare.backgroundColor = UIColor.blueColor()
        self.coloredSquare.frame = CGRect(x: 0, y: 500, width: 50, height: 50)
        }, completion: { animationFinished in
            self.movementUp()
    })
}

func movementUp() {
    UIView.animateWithDuration(1.0, animations: {
        self.coloredSquare.backgroundColor = UIColor.redColor()
        self.coloredSquare.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        }, completion: { animationFinished in
            self.movementDown()
    })
}

如果我尝试使用 WHILE 执行一个方法直到条件为假,Xcode 构建但模拟器停止在启动屏幕,如果我取消 while 条件,动画工作但没有任何停止... 谁能帮我? 谢谢

【问题讨论】:

    标签: ios animation swift uiview


    【解决方案1】:

    首先,摆脱 while 循环。在动画的完成块中,在调用其他动画之前首先检查动画是否完成——如果取消动画,它不会是真的,所以动画会停止。在您的按钮方法中,您需要访问coloredSquare的表示层以获取其当前位置,并取消所有动画以使动画立即停止。

    class ViewController: UIViewController {
    
    var coloredSquare: UIView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        [self .movement()];
    }
    
    
    func movement() {
    
        coloredSquare.backgroundColor = UIColor.blueColor()
    
        coloredSquare.frame = CGRect(x:0, y:500, width:50, height:50)
    
        self.view.addSubview(coloredSquare)
    
        movementDown()
    }
    
    func movementDown() {
    
        UIView.animateWithDuration(3.0, animations: {
            self.coloredSquare.backgroundColor = UIColor.blueColor()
            self.coloredSquare.frame = CGRect(x: 0, y: 500, width: 50, height: 50)
            }, completion: { animationFinished in
                if animationFinished {
                    self.movementUp()
                }
        })
    }
    
    func movementUp() {
        UIView.animateWithDuration(3.0, animations: {
            self.coloredSquare.backgroundColor = UIColor.redColor()
            self.coloredSquare.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
            }, completion: { animationFinished in
                if animationFinished {
                    self.movementDown()
                }
        })
    }
    
    
    
    @IBAction func stopBlock(sender: AnyObject) {
        var layer = coloredSquare.layer.presentationLayer() as CALayer
        var frame = layer.frame
        println(frame)
        coloredSquare.layer.removeAllAnimations()
        coloredSquare.frame = frame // You need this to keep the block where it was when you cancelled the animation, otherwise it will jump to the position defined by the end of the current animation
    }
    

    }

    【讨论】:

      【解决方案2】:

      您需要的是一个定期调用的函数,这样系统就不会在等待您的 while 循环结束时被锁定。

      您可以设置一个计时器来运行您的animation,如下所示:

      NSTimer *animationTimer;
      NSTimeInterval animationInterval = 1.0 / 30.0;
      
      - (void) startAnimation{
          animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(animate) userInfo:nil repeats:YES];
      }
      
      - (void) stopAnimation{
          [animationTimer invalidate];
          animationTimer = nil;
      }
      
      - (void) animate {
          NSLog(@"do your animation here..");
      }
      

      【讨论】:

        【解决方案3】:

        因为你的 while 循环无法被打破。

        你陷入了无法打破的无限循环。

        buttonPress == true 是不可能的。

        您需要在 while 循环中检查 buttonPress。

        但是,我根本不建议使用这种方法。

        你应该这样做:

        创建一个使正方形向上的动画。让它在动画完成时触发回调,并让它动画方块到底部。这样你就不必使用while循环了。

        虽然循环在主线程上可能非常危险(如您当前所见)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多