IOS animation effect

Source: Internet
Author: User

1. Use nstimer for animation

1. Create an empty application and add the homeviewcontroller page, iphone.png Image

2. Add an image view in homeviewcontroller. XIB and adjust its size. Then add a slider control.

 

3. homeviewcontroller. H code:

# Import <uikit/uikit. h>

@ Interface homeviewcontroller: uiviewcontroller {cgpoint delta; // coordinate variation nstimer * timer; cgsize picsize; // image size} @ property (retain, nonatomic) iboutlet uiimageview * imageview; @ property (retain, nonatomic) iboutlet uislider * slider;-(ibaction) sliderchanged :( ID) sender; @ end

Homeviewcontroller. m code:

# Import "homeviewcontroller. H"

@ Interface homeviewcontroller () @ end @ implementation homeviewcontroller

 

@ Synthesize imageview; @ synthesize slider;-(void) move {imageview. center = cgpointmake (imageview. center. X + delta. x, imageview. center. Y + delta. y); If (imageview. center. x> self. view. bounds. size. width-picsize. width/2 | imageview. center. x <picsize. width/2) {delta. X =-Delta. x;} If (imageview. center. y> self. view. bounds. size. height-picsize. height/2 | imageview. center. Y <picsize. height/2) {delta. y =-Delta. y;}/* we constantly change the center coordinates of imaageview. the horizontal and vertical adjustment values are the x and y values of delta, when we detect that the position of the imageview exceeds or is smaller than the screen width or height, we change its motion direction */}-(void) viewdidload {picsize = imageview. bounds. size; Delta = cgpointmake (8.0, 4.0); timer = [nstimer scheduledtimerwithtimeinterval: slider. value Target: Self selector: @ selector (MOVE) userinfo: Nil repeats: Yes]; [Super viewdidload];}-(void) dealloc {

 

[Timer invalidate]; [imageview release]; [slider release]; [Super dealloc];}

 

-(Ibaction) sliderchanged :( ID) sender {[Timer invalidate]; timer = [nstimer scheduledtimerwithtimeinterval: slider. value Target: Self selector: @ selector (MOVE) userinfo: Nil repeats: Yes];} @ end

Because the images are static, they do not have any effect. This is very fun. iPhone pictures will be moved around different locations on the phone, and the speed can be adjusted.

Ii. Visual effect Animation

Make the animation smooth:

 

Modify the move Method

-(Void) move {[uiview beginanimations: @ "myanimation" context: Nil]; [uiview setanimationduration: slider. value]; // animation execution time [uiview setanimationcurve: uiviewanimationcurvelinear]; // set the animation execution speed/* setanimationcurve has four constants: uiviewanimationcurvelinear within the animation execution time, the speed is always the same. Uiviewanimationcurveeaseinout: When an animation is executed, the speed starts to slow down, and then the speed starts to slow down at the end. uiviewanimationcurveeasein starts to slow down gradually until the speed of uiviewanimationcurveeaseout is completed, then gradually slow down until the end */imageview. center = cgpointmake (imageview. center. X + delta. x, imageview. center. Y + delta. y); [uiview commitanimations]; // call when the animation block ends. At this time, the imageview will be very smooth when it is moved from one point to another, instead of skipping from one point to another. If (imageview. center. x> self. view. bounds. size. width-picsize. width/2 | imageview. center. x <picsize. width/2) {delta. X =-Delta. x;} If (imageview. center. y> self. view. bounds. size. height-picsize. height/2 | imageview. center. Y <picsize. height/2) {delta. y =-Delta. y;}/* we constantly change the center coordinates of imaageview. the horizontal and vertical adjustment values are the x and y values of delta, when we detect that the position of the imageview is greater than or less than the screen width or height, we change its motion direction */}

3. View Deformation

Although nstimer can be used to achieve the animation effect of moving the view position, you can also use another technology provided by the ios sdk-Transforms (defined in the core graphics framework) to achieve the same effect, and more functions are implemented.

Including: translation, moving the origin coordinates of the view to the specified position; rotation, rotating the view to a specified angle; scaling, scaling the view to a specified width/height ratio.

 

1. Displacement Animation

Modify homeviewcontroller. h

# Import <uikit/uikit. h> @ interface homeviewcontroller: uiviewcontroller {cgpoint delta; nstimer * timer; cgsize picsize; cgpoint translation ;}@ property (retain, nonatomic) iboutlet uiimageview * imageview; @ property (retain, nonatomic) iboutlet uislider * slider;-(ibaction) sliderchanged :( ID) sender;

@ End

Modify the move and viewdidload methods of homeviewcontroller. M.

-(Void) move {imageview. transform = cgaffinetransformmaketranslation (translation. x, translation. y); // move the view to the specified position. X + = delta. x; translation. Y + = delta. y; If (imageview. center. X + translation. x> self. view. bounds. size. width-picsize. width/2 | imageview. center. X + translation. x <picsize. width/2) {delta. X =-Delta. x;} If (imageview. center. Y + translation. y> self. view. bounds. size. height-picsize. height/2 | imageview. center. Y + translation. Y <picsize. height/2) {delta. y =-Delta. Y ;}}

-(Void) viewdidload

{Picsize = imageview. bounds. size; Delta = cgpointmake (8.0, 4.0); translation = cgpointmake (0.0, 0.0); timer = [nstimer scheduledtimerwithtimeinterval: slider. value Target: Self selector: @ selector (MOVE) userinfo: Nil repeats: Yes]; [Super viewdidload];}

 

2. Rotating Animation

Modify homeviewcontroller. h

# Import <uikit/uikit. h> @ interface homeviewcontroller: uiviewcontroller {cgpoint delta; nstimer * timer; cgsize picsize; cgpoint translation; float angle; // Rotation Angle} @ property (retain, nonatomic) iboutlet uiimageview * imageview; @ property (retain, nonatomic) iboutlet uislider * slider;-(ibaction) sliderchanged :( ID) sender;

@ End

Modify the move and viewdidload methods of homeviewcontroller. M.

-(Void) move {imageview. transform = cgaffinetransformmakerotation (angle); angle + = 0.02;/* determines whether the angle is greater than 360 °. If the angle is greater than 2 π (3.14159*2 = 6.28318 ), set angle to 0 */If (angle> 6.2832) {angle = 0 ;}}

-(Void) viewdidload

{Picsize = imageview. bounds. size; Delta = cgpointmake (8.0, 4.0 );

 

Translation = cgpointmake (0.0, 0.0 );

Angle = 0;

Timer = [nstimer scheduledtimerwithtimeinterval: slider. Value Target: Self selector: @ selector (MOVE) userinfo: Nil repeats: Yes]; [Super viewdidload];}

3. Zoom Animation

You only need to modify the sliderchanged event of the slider.

-(Ibaction) sliderchanged :( ID) sender {imageview. Transform = cgaffinetransformmakescale (slider. Value, Slider. Value); // zoom

}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.