ios UIView animation curves
Asked Answered
C

3

17

Is there any way to do things the "short and easy" way like below? The curve appears to still use EaseOut.

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView animateWithDuration:0.5 animations:^{
    // ... do stuff here
}];
Chlorite answered 11/6, 2012 at 19:50 Comment(0)
L
32

You are mixing two different kinds of UIView-animations. You should be using something like this either of these:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                    // ... do stuff here
               } completion:NULL];

This came from the newer block-based UIView-animation API. The first line, on the other hand, is part of the older UIView animation API that looks like this:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
// ... do stuff here
[UIView commitAnimations];

They do the same thing and you can use either (though Apple recommends the block-based API since it is easier/cleaner to do callbacks after the animation finishes).

Leotaleotard answered 11/6, 2012 at 20:12 Comment(0)
D
2

You can do it using Core Animation , CAKeyFrameAnimation to define the curve points. See this tutorial: http://nachbaur.com/blog/core-animation-part-4

Derbent answered 10/7, 2013 at 9:42 Comment(2)
Note that link-only answers are discouraged here on SO. Please consider editing your answer and adding a synopsis here.Disrepair
@NAZIK your honor i am here to answer question and find a proper solution to querry!Derbent
D
0

Let's take curve points p1, p2, p3, p4 and p5, & find mid point for each pair of adjacent points. Label m1 as the mid point for p1 and p2. Similarly for m2, m3, m4.

  • Add quad curve to point m2 with p2 as a control point.
  • Add quad curve to point m3 with p3 as a control point.
  • Add quad curve to point m4 with p4 as a control point.

Code:

CGFloat screenHeight = self.view.frame.size.height;
CGFloat screenWidth = self.view.frame.size.width;

UIView *aniView = [[UIView alloc] initWithFrame:CGRectMake(50, screenHeight, 50, 50)];
[aniView setBackgroundColor:[UIColor redColor]];
aniView.layer.cornerRadius = 25.0;
[self.view addSubview:aniView];


UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:aniView.center];
[movePath addQuadCurveToPoint:CGPointMake(screenWidth-50,screenHeight-50)
                 controlPoint:CGPointMake(screenWidth/2,screenHeight-150)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim,  nil];
animGroup.duration = 2.0;

[CATransaction begin]; {
    [CATransaction setCompletionBlock:^{
        [aniView.layer removeAllAnimations];
        [aniView removeFromSuperview];
    }];

    [aniView.layer addAnimation:animGroup forKey:nil];

} [CATransaction commit];

Copy past above code into some method and try calling the method...

Disarticulate answered 30/8, 2017 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.