Skip to content

Commit cd08cb6

Browse files
committed
very reluctantly, demonstrate how to use setAnimationRepeatCount with new-style animation blocks
1 parent ea5a62a commit cd08cb6

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

bk2ch04p130viewAnimation/bk2ch04p130viewAnimation/ViewController.swift

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,34 @@ class ViewController: UIViewController {
140140
}
141141
}
142142

143+
let whichAnimateWay = 2 // 1 or 2
144+
143145
func animate(count:Int) {
144-
let opts = UIViewAnimationOptions.Autoreverse
145-
let xorig = self.v.center.x
146-
UIView.animateWithDuration(1, delay: 0, options: opts, animations: {
147-
self.v.center.x += 100
148-
}, completion: {
149-
_ in
150-
self.v.center.x = xorig
151-
if count > 1 {
152-
self.animate(count-1)
153-
}
154-
})
146+
switch whichAnimateWay {
147+
case 1:
148+
let opts = UIViewAnimationOptions.Autoreverse
149+
let xorig = self.v.center.x
150+
UIView.animateWithDuration(1, delay: 0, options: opts, animations: {
151+
UIView.setAnimationRepeatCount(Float(count)) // I really don't like this
152+
self.v.center.x += 100
153+
}, completion: {
154+
_ in
155+
self.v.center.x = xorig
156+
})
157+
case 2:
158+
let opts = UIViewAnimationOptions.Autoreverse
159+
let xorig = self.v.center.x
160+
UIView.animateWithDuration(1, delay: 0, options: opts, animations: {
161+
self.v.center.x += 100
162+
}, completion: {
163+
_ in
164+
self.v.center.x = xorig
165+
if count > 1 {
166+
self.animate(count-1)
167+
}
168+
})
169+
default: break
170+
}
155171
}
156172

157173
}

0 commit comments

Comments
 (0)