每次按下按钮时,我希望能够将对象(在我的情况下,图像"小狗")向上移动1个像素.我偶然发现了旧的Objective-C解决方案以及类似的Swift代码,但没有一个适合我的具体问题.我很想知道移动图像的简单方法.这是我的代码到目前为止我可以收集的内容(我希望它不必要地长,可以减少到一两行):
@IBAction func tapButton() {
UIView.animateWithDuration(0.75, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.puppy.alpha = 1
self.puppy.center.y = 0
}, completion: nil)
var toPoint: CGPoint = CGPointMake(0.0, 1.0)
var fromPoint : CGPoint = CGPointZero
var movement = CABasicAnimation(keyPath: "movement")
movement.additive = true
movement.fromValue = NSValue(CGPoint: fromPoint)
movement.toValue = NSValue(CGPoint: toPoint)
movement.duration = 0.3
view.layer.addAnimation(movement, forKey: "move")
}
Run Code Online (Sandbox Code Playgroud) 我用伪代码写了一些,因为我不知道它的语法.我想知道timeLeftLabel.text在6小时结束之前剩下多少小时,分钟和秒.我最大的问题是我不知道如何加减次数.谁能帮我?
var timer = NSTimer()
func timerResults() {
let theDate = NSDate()
var endTime = theDate //+ 6 hours
let timeLeft = endTime //- theDate
timeLeftLabel.text = "\(timeLeft)"
}
@IBOutlet weak var timeLeftLabel: UILabel!
@IBAction func IBbtnUpdateTap(sender: UIButton){
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("timerResults"), userInfo: nil, repeats: true)
}
Run Code Online (Sandbox Code Playgroud)