Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to custom UIViewAnimationCurve like CAMediaTimingFunction's functionWithControlPoints

the UIViewAnimationCurve only have

  • UIViewAnimationCurveEaseInOut
  • UIViewAnimationCurveEaseIn
  • UIViewAnimationCurveEaseOut
  • UIViewAnimationCurveLinear

these functions.

but,I need like ExpoOut , BackInOut animation for UIView.

like ExpoOut is [CAMediaTimingFunction functionWithControlPoints:0.12 :0.51 :-0.4 :1];

I has used CABasicAnimation,but it can not change frame like UIView animation,it very bad when change the view size.

or,are you have any better way to change view frame like real,not look like zoomin.

thanks.

like image 318
yellow Avatar asked Mar 28 '12 10:03

yellow


2 Answers

A problem here is that it's impossible to specify a timing function for a UIView-based animation. As far as I can tell you're then left with a slew of unpleasant options:

  • Switch to using CALayer based animation—with the serious problem that if the delegate is set to a view, the animation doesn't happen. As this is required for UIViews, that's an unappealing option.
  • Add parent layer to your views, and animate that layer—with the equally serious problem that this layer will be nowhere in the nib, and modifications to the nib may inadvertently break the animations.
  • Don't use a custom timing function.

I hacked my way by setting the delegate to nil right before animating, and setting back to the UIView right after, but it feels very dirty. I may be missing something, though. My code looks something like this:

- (void)methodThatAnimates {
    [CATransaction begin]; {
        /* Must set to nil, otherwise the view just jumps to the new location. */
        self.viewToAnimate.layer.delegate = nil;

        CAMediaTimingFunction *timingFunction = 
            [CAMediaTimingFunction functionWithControlPoints:0.4 :0 :0 :1.0];
        [CATransaction setAnimationTimingFunction:timingFunction]
        self.viewToAnimate.layer.position = CGPoint(15.0, 0.0);

        /* Set delegate back to the view, as is required per the 
           Apple documentation. */
        self.viewToAnimate.layer.delegate = self.viewToAnimate;
    } [CATransaction commit];
}

So far, it seems to work like a charm, but I keep expecting some bizarre artifact to show up because of the temporarily nil delegate.

like image 104
nomothetis Avatar answered Nov 05 '22 00:11

nomothetis


You might want to take a look at MTAnimation. It's a UIView Category that provides jQuery like timing functions.

like image 32
voidStern Avatar answered Nov 04 '22 23:11

voidStern