简体   繁体   中英

animateWithDuration is not showing

I'm learning Swift and I'm having problems moving a UIView from different functions. If I use this:

ViewController:

let boardView = BoardView()

@IBAction func startButtonTapped(sender: UIButton) {
    view.addSubview(boardView.background)
}

The class BoardView:

class BoardView : UIView {

   let multiplier: CGFloat = 0.95
   let screenSize = UIScreen.mainScreen().bounds

   var background: UIView {
      let boardSize: CGFloat = multiplier * screenSize.width
      let board = UIView(
         frame: CGRectMake(-boardSize,
         screenSize.height / 2 - boardSize / 2,
         boardSize,
         boardSize)
      )

       board.layer.cornerRadius = 8.0
       board.backgroundColor = UIColor.blackColor()
       UIView.animateWithDuration(0.15, animations: {
          board.center.x = self.screenSize.width / 2
      })
      return board
   }
}

The animation works (in an counterintuitive way, for me). If I try to move the UIView from another function, like this:

ViewController button:

@IBAction func startButtonTapped(sender: UIButton) {
    view.addSubview(boardView.background)
    boardView.moveBackground()
}

BoardView:

class BoardView : UIView {

let multiplier: CGFloat = 0.95
let screenSize = UIScreen.mainScreen().bounds

var background: UIView {
    let boardSize: CGFloat = multiplier * screenSize.width
    let board = UIView(
        frame: CGRectMake(-boardSize,
        screenSize.height / 2 - boardSize / 2,
        boardSize,
        boardSize))

    board.layer.cornerRadius = 8.0
    board.backgroundColor = UIColor.blackColor()
    return board
}

func moveBackground() {
    UIView.animateWithDuration(0.15, animations: {
        self.background.center.x = self.screenSize.width / 2
    })
 }
}

The animation doesn't work. Nothing happens either if I use animateWithDuration in the ViewController.

How can I move an UIView from a different place than where I've created it?

Your original code, while strangely working, is in fact poorly defined behavior. You should not start animating a view before adding it to the view hierarchy. You're getting away with it but don't do that.

In your second approach, which is closer to correct, the problem is that you've made background a non-lazy computed property. So every time you call model.background , you get a different object.

Given how you've written this, a lazy property for background is probably what you really mean.

As @dasdom notes, BoardModel is a very bad name for a view. This should be BoardView .

In general, your flow is a bit strange. I would probably have the BoardView add the background to itself. If the controller is actually adding the background to some other view (other than BoardView ) then that's really bizarre, and you need to rethink your code structure.

When ever you access the background of the boardModel (which is a really bad name for a view!), a new view is created and returned. I don't think this is what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM