Use of iOS advanced-quartzcore framework-coreanimation and UIView animations

Source: Internet
Author: User

First, the animation of the UIView package
There are 2 fatal drawbacks to layer animations:
1> will bounce by default.
2> The animation you see is an illusion, the properties of the layer have never changed.
So the suggestion can use the UIView to use the UIView, really does not use the layer.
1.UIView Mobile Animation
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
[UIView Beginanimations:nil Context:nil];
When the animation finishes executing, the self's Animatestop method is automatically called (In this way, the execution of the animation is monitored
Finished
[UIView setanimationdelegate:self];
[UIView setanimationdidstopsecletor: @selector (animatestop)];
Self.myView.center = Cgpointmake (200,300);
[UIView commitanimations];
}

Transition animations for 2.UIView
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
[UIView transitionWithView:self.iconView duration:1.0 options:
Uiviewanimationoptiontransitionflipfromtop Animations:nil
Completion:nil];
}

Second, block animation
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
[UIView animatewithduration:1.0 animations:^{
Self.myView.center = Cgpointmake (200,300);
} completion:^ (BOOL finished) {
Here, the execution of the listening animation is complete.
}];
}

Third, example: turntable

1. Customize a View package carousel.
2. Create a xib to describe the turntable. (because it is fixed, so use xib)
3. Provides a class method to quickly create a turntable. (Load Xib in this method)
+ (instancetype) wheel
{
return [[[NSBundle Mainbundle] loadnibnamed:@ "Mjwheel" Owner:nil
Options:nil] Lastobject];
}
4. Provides a way to set the animation effect of rotation
began to spin and keep spinning
-(void) startrotating
{
if (Self.link) return;
1 seconds to refresh 60 times
Cadisplaylink *link = [Cadisplaylink displaylinkwithtarget:self
Selector: @selector (update)];
[Link Addtorunloop:[nsrunloop Mainrunloop]
Formode:nsdefaultrunloopmode];
}

-(void) update
{
Self.centerWheel.transform = cgaffinetrasformrotate:
(self.centerwheel.transform,m_pi/500);
}
PS: Because with the core animation is false, may cause later point is not allowed, and into the background after the animation will stop,
So do not take the core animation this way

5. In the viewdidload of the controller, call the class method to create the carousel
{
[Super Viewdidload];
Mjwheel *wheel = [Mjwheel wheel];
Wheel.center = Cgpointmake (Self.view.frame.width *
0.5,self.view.frame.height * 0.5);
[Wheel startrotating];
[Self.view Addsubview:wheel];
}

6. Come to Mjwheel, rewrite the Awakefromnib method (add a button in this method)
The property that is obtained in this method is a value
-(void) awakefromnib
{
ImageView default cannot interact with user, so set to Yes first
self.centerWheel.userInteractionEnabled = YES;

Load large picture
UIImage *bigimage = [UIImage imagenamed:luckyastrology];
UIImage *bigimageselected = [UIImage Imagenamed:luckyastrologyp];

Crop a picture of a constellation from a large picture
CGFloat SMALLW = BIGIMAGE.SIZE.WIDTH/12 * [UIScreen Mainscreen].scale;
CGFloat SMALLH = bigImage.size.height * [UIScreen Mainscreen].scale;

for (int index = 0; index<12; index++) {
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];


CGRect smallrect = CGRectMake (index * SMALLW,0,SMALLW,SMALLH);

Cgimagecreatewithimageinrect: Method only recognize pixels
Cgimageref smallimage = cgimagecreatewithimageinrect:
(Bigimage.cgimage, Smallrect);
Cgimageref smallimageselected = cgimagecreatewithimageinrect:
(Bigimageselected.cgimage, Smallrect);

[Btn Setimage:[uiimage Imagewithcgimage:smallimage]
Forstate:uicontrolstatenormal];
[Btn Setimage:[uiimage Imagewithcgimage:smallimage]
Forstate:uicontrolstateselected];
[Btn Setbackgroundimage:[uiimage
imagenmaed:@ "luckyrototeselected"] forstate:uicontrolsatateselected];

Btn.bounds = CGRectMake (0,0,68,143);

Setting anchor points and locations
Btn.layer.anchorPoint = Cgpointmake (0.5,1);
Btn.layer.position = Cgpointmake (self.centerWheel.frame.size.width
*0.5,self.centerwheel.frame.size.height *0.5);

Set the rotation angle (rotate around the anchor point)
CGFloat angle = (index)/180.0 *m_pi;
Btn.transform = cgaffinetransformmakerotation (angle);

Monitor button click
[Btn addtarget:self Action: @selector (Btnclick:)
Forcontrolevents:uicontroleventtouchdown];

[Self.centerwheel ADDSUBVIEW:BTN];

if (index = = 0) {
[Self btnclick:btn];
}
}
}

Note: UIImage is calculated by point, cgimage is calculated by pixel, the latter is twice times the former.
7. Add a button property selectedbtn Save the selected buttons
Implementing a Listening method
-(void) Btnclick: (UIButton *) btn
{
self.selectedBtn.selected = NO;
btn.selected = YES;
SELF.SELECTEDBTN = BTN;
}

8. Adjust the size of the internal imageview of the button
1> Custom UIButton
2> overriding Imagerectforcontextrect: Methods
{
CGFloat Imagew = 40;
CGFloat Imageh = 47;
CGFloat ImageX = (contentrect.size.width-imagew) * 0.5;
CGFloat imagey = 20;

return CGRectMake (Imagex,imagey,imagew,imageh);
}
3> Press the button and don't want it to turn gray---rewrite sethighlighted: Method (Nothing to write)

9. Start the Pick number
1> Monitor Start Select button and implement method

Use of iOS advanced-quartzcore framework-coreanimation and UIView animations

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.