|
| 1 | +# CardAnimation |
| 2 | +[Design from Dribble](https://dribbble.com/shots/1265487-First-shot-in-Chapps-Animation) |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +**Change Anchor Point of CALayer** |
| 8 | + |
| 9 | +like: (0.5, 0.5) ->(0.5, 1) |
| 10 | + |
| 11 | +Frame: |
| 12 | + |
| 13 | +There are many ways to do this: [link](http://stackoverflow.com/questions/1968017/changing-my-calayers-anchorpoint-moves-the-view) |
| 14 | + |
| 15 | + subView.frame = frame |
| 16 | + subView.layer.anchorPoint = CGPointMake(0.5, 1) |
| 17 | + subView.frame = frame |
| 18 | + |
| 19 | +AutoLayout: |
| 20 | + |
| 21 | +Discussion on stackoverflow: [link](http://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used/14105757#14105757), but I find a simple way: |
| 22 | + |
| 23 | + let subViewHeight = |
| 24 | + let oldConstraintConstant = centerYConstraint.constant |
| 25 | + subView.layer.anchorPoint = CGPointMake(0.5, 1) |
| 26 | + //Like what you do with frame, you need to compensate for additional translation. |
| 27 | + centerYConstraint.constant = subView.bounds.size.height/2 + oldConstraintConstant |
| 28 | + |
| 29 | +**Transform and AutoLayout** |
| 30 | + |
| 31 | +From iOS8, transform and autolayout play nice. There is a blog for this: [Constraints & Transformations](http://revealapp.com/blog/constraints-and-transforms.html) |
| 32 | + |
| 33 | +Transform doesn't affect autolayout, only constraints can affect autolayout. |
| 34 | + |
| 35 | +Transformes affect view's frame, but do nothing to view's center and bounds. |
| 36 | + |
| 37 | +**Make flip animation background not transparent** |
| 38 | + |
| 39 | +Use a subview, and change the container view's background color to what color you want. |
| 40 | + |
| 41 | +When the container view is vertical to screen, make the subview hidden, and after the container view back, make subview visible. |
| 42 | + |
| 43 | +**Rotation Animation Bug in action method** |
| 44 | + |
| 45 | + let flipTransform = CATransform3DRotate(CATransform3DIdentity, CGFloat(-M_PI), 1, 0, 0) |
| 46 | + UIView.animateWithDuration(0.3, { |
| 47 | + view.layer.transform = flipTransform |
| 48 | + }) |
| 49 | + |
| 50 | +The animation will not execute and the view just change if you execute above code in an action method, like clip a button. |
| 51 | +You could use 'CGFloat(-M_PI) * 0.99' to fix this. |
| 52 | + |
| 53 | +**To-Do List** |
| 54 | + |
| 55 | +1. reuse card view |
| 56 | + |
| 57 | +2. reorder card view |
| 58 | + |
| 59 | +3. delete and add card view with pan gesture |
0 commit comments