UIView.animateWithDuration swift 3

5

当按钮被按下时它会起作用。点击后,此函数将显示另一个视图。

@IBAction func charSetPressed(_ button: UIButton) {
    if button.titleLabel!.text == "1/2" {

        charSet1.isHidden = true
        charSet2.isHidden = false

        button.setTitle("2/2", for: .normal)

    } else if button.titleLabel!.text == "2/2" {
        charSet1.isHidden = false
        charSet2.isHidden = true
        button.setTitle("1/2", for: .normal)
    }

    UIView.animateWithDuration(0.2, animations: {

        button.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0)
        }, completion: {(_) -> Void in(here the error happend)

            button.transform =
            CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)
    })
}

1
什么是错误,你的错误在哪里? - Anbu.Karthik
请展示您的错误日志。 - Himanshu Moradiya
请查看此链接 https://dev59.com/ilkS5IYBdhLWcg3wV1dy,了解一些新的Swift3语法。 - AMAN77
请点击链接查看图片。 - user7003741
2个回答

7

//按键时动画...(适用于Swift 3.0)

    UIView.animate(withDuration: 0.2, animations: {
        button.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
    }, completion:{ _ in
        button.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })

4

结果:


代码:

import UIKit
import Foundation

class ViewController: UIViewController {
  
  @IBOutlet weak var myView: UIView!
  
  @IBAction func buttonTouched(_ sender: AnyObject) {
    
    // animate scaling by 2.0, 2.0
    UIView.animate(withDuration: 0.2, animations: {
      let transformScaled = CGAffineTransform
                                          .identity
                                          .scaledBy(x: 2.0, y: 2.0)

      self.myView.transform = transformScaled
    }) { (finished) in
      // once finished first animation
      // start second animation
      if finished {
        // animate scaling by 1.0, 1.0
        UIView.animate(withDuration: 0.2, animations: { 
          let transformScaled = CGAffineTransform
                                              .identity
                                              .scaledBy(x: 1.0, y: 1.0)
          
          self.myView.transform = transformScaled
        })
      }
    }
    
  }
  
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接