|
| 1 | +/* |
| 2 | + Copyright 2017-present The Material Motion Authors. All Rights Reserved. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import UIKit |
| 18 | +import MotionAnimator |
| 19 | + |
| 20 | +// This demo shows how to the MotionAnimator UIKit-ish APIs for animating properties and is provided |
| 21 | +// as a contrast to the TapToBounceTraits example. |
| 22 | +class TapToBounceUIKitExampleViewController: UIViewController { |
| 23 | + |
| 24 | + override func viewDidLoad() { |
| 25 | + super.viewDidLoad() |
| 26 | + |
| 27 | + view.backgroundColor = .white |
| 28 | + |
| 29 | + let circle = UIButton() |
| 30 | + circle.bounds = CGRect(x: 0, y: 0, width: 128, height: 128) |
| 31 | + circle.center = view.center |
| 32 | + circle.layer.cornerRadius = circle.bounds.width / 2 |
| 33 | + circle.layer.borderColor = UIColor(red: (CGFloat)(0x88) / 255.0, |
| 34 | + green: (CGFloat)(0xEF) / 255.0, |
| 35 | + blue: (CGFloat)(0xAA) / 255.0, |
| 36 | + alpha: 1).cgColor |
| 37 | + circle.backgroundColor = UIColor(red: (CGFloat)(0xEF) / 255.0, |
| 38 | + green: (CGFloat)(0x88) / 255.0, |
| 39 | + blue: (CGFloat)(0xAA) / 255.0, |
| 40 | + alpha: 1) |
| 41 | + view.addSubview(circle) |
| 42 | + |
| 43 | + circle.addTarget(self, action: #selector(didFocus), |
| 44 | + for: [.touchDown, .touchDragEnter]) |
| 45 | + circle.addTarget(self, action: #selector(didUnfocus), |
| 46 | + for: [.touchUpInside, .touchUpOutside, .touchDragExit]) |
| 47 | + } |
| 48 | + |
| 49 | + func didFocus(_ sender: UIButton) { |
| 50 | + MotionAnimator.animate(withDuration: 0.8, |
| 51 | + delay: 0, |
| 52 | + usingSpringWithDamping: 0.5, |
| 53 | + initialSpringVelocity: 0, |
| 54 | + options: [], |
| 55 | + animations: { |
| 56 | + sender.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) |
| 57 | + |
| 58 | + // This would normally not be animatable with the UIView animation APIs, but it is animatable |
| 59 | + // with the motion animator. |
| 60 | + sender.layer.borderWidth = 10 |
| 61 | + }, completion: nil) |
| 62 | + } |
| 63 | + |
| 64 | + func didUnfocus(_ sender: UIButton) { |
| 65 | + MotionAnimator.animate(withDuration: 0.8, |
| 66 | + delay: 0, |
| 67 | + usingSpringWithDamping: 0.5, |
| 68 | + initialSpringVelocity: 0, |
| 69 | + options: [], |
| 70 | + animations: { |
| 71 | + sender.transform = .identity |
| 72 | + sender.layer.borderWidth = 0 |
| 73 | + }, completion: nil) |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments