iOS version big turntable

Source: Internet
Author: User
Tags reserved uikit
iOS version big turntable

Using the Cabasicanimation animation with iOS to achieve a simple, core code :

    Rotating animation
    Cabasicanimation *anim = [cabasicanimation animationwithkeypath:@ "transform.rotation"];
    Start position
    [Anim Setfromvalue:[nsnumber numberwithfloat:0]];
    End
    [Anim settovalue:[nsnumber NUMBERWITHFLOAT:M_PI * 2.97]];
    Anim.duration = 5.0;
    Animation end don't go back to start position
    anim.removedoncompletion = no;
    Anim.fillmode = kcafillmodeforwards;
    [Anim setdelegate:self];
    Mode: First acceleration after deceleration
    [Anim settimingfunction:[camediatimingfunction functionwithname: Kcamediatimingfunctioneaseineaseout]];
    [Imageview.layer addanimation:anim forkey:@ "Rotation"];

When rotating, the m_pi is half circle, M_PI * 2 is a lap;
The turntable is equally divided, such as items = 7;
Get stop position from server, such as Resultitem = 3;
Calculate the stopping angle for orign = Resultitem/items * 2 * M_PI;

Core on the purple, on this basis to expand the next two, expand

. h

//  ViewController.h
//  demo
////
Created by ZHANGWB on  15/7/22.  Copyright (c) 2015 lxmy. All rights reserved.

#import <UIKit/UIKit.h>

@interface viewcontroller:uiviewcontroller

/** Rotating Position * *
Property (Nonatomic, Strong) Uisegmentedcontrol *segmenteditem;
/** the direction of rotation * * *
@property (nonatomic, strong) Uisegmentedcontrol *segmenteddirection;
/** Hint Text * * *
@property (nonatomic, strong) Uilabel *lblresult;
/** chassis *
/@property (nonatomic, strong) Uiimageview *imground;
/** pointer *
/@property (nonatomic, strong) UIButton *btnindex;

@end

. m

VIEWCONTROLLER.M//demo////Created by ZHANGWB on 15/7/22. Copyright (c) 2015 lxmy.
All rights reserved. Examples of large turntable//#import "ViewController.h" #import "Masonry.h" Const static CGFloat anim_time = 5.0; Animation time const static CGFloat rotation_extend = 20; Number of rotation cycles//2 * M_PI = 1 Cycles const static Nsinteger item_count = 14;

    Turntable @interface Viewcontroller () {Uialertview *alert; Boolean ISCW; Whether the clockwise float random; Position, starting from 0 float orign in demo;

    Angle} @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
[Self addview];

}-(void) didreceivememorywarning {[Super didreceivememorywarning];} /** Add control/-(void) AddView {if ([Self respondstoselector: @selector (edgesforextendedlayout)]) {SELF.EDGESFO
    Rextendedlayout = Uirectedgenone;

    } Self.title = @ "Big turntable";
    Nsarray *arrayitem = [[Nsarray alloc]initwithobjects:@ "pointer rotation" @ "chassis rotation", nil]; _segmenteditem = [[Uisegmentedcontrol alloc]inItwithitems:arrayitem];
    _segmenteditem.selectedsegmentindex = 0; [_segmenteditem addtarget:self Action: @selector (segmentedvaluechanged) forControlEvents:
    Uicontroleventvaluechanged];
    _segmenteditem.translatesautoresizingmaskintoconstraints = NO;

    [Self.view Addsubview:_segmenteditem];
    Nsarray *arraydirection = [[Nsarray alloc]initwithobjects:@ "counterclockwise", @ "clockwise", nil];
    _segmenteddirection = [[Uisegmentedcontrol alloc]initwithitems:arraydirection];
    _segmenteddirection.selectedsegmentindex = 0; [_segmenteddirection addtarget:self Action: @selector (segmenteddirectionvaluechanged) forControlEvents:
    Uicontroleventvaluechanged];
    _segmenteddirection.translatesautoresizingmaskintoconstraints = NO;

    [Self.view addsubview:_segmenteddirection];
    _lblresult = [Uilabel new];
    _lblresult.textalignment = Nstextalignmentcenter;
    _lblresult.translatesautoresizingmaskintoconstraints = NO;

    [Self.view Addsubview:_lblresult];
 _imground = [Uiimageview new];   _imground.image = [UIImage imagenamed:@ "Round"];
    _imground.translatesautoresizingmaskintoconstraints = NO;

    [Self.view Addsubview:_imground];
    _btnindex = [UIButton buttonwithtype:uibuttontypecustom];
    [_btnindex setimage:[uiimage imagenamed:@ "Start"] forstate:uicontrolstatenormal];
    [_btnindex addtarget:self Action: @selector (Btnindexclick) forcontrolevents:uicontroleventtouchupinside];
    _btnindex.translatesautoresizingmaskintoconstraints = NO;

    [Self.view Addsubview:_btnindex];

    int padding = 10; [_segmenteditem mas_makeconstraints:^ (Masconstraintmaker *make)
        {Make.top.equalTo (self.view.mas_top). Offset (padding * 5);
    Make.centerX.equalTo (Self.view.mas_centerX);

    }]; [_segmenteddirection mas_makeconstraints:^ (Masconstraintmaker *make)
        {Make.top.equalTo (_segmenteditem.mas_bottom). offset (padding);
    Make.centerX.equalTo (Self.view.mas_centerX);

    }]; [_lblresult mas_makeconstraints:^ (Masconstraintmaker *make) {Make.top.equalTo (_segmenteddirection.mas_bottom). offset (padding);
        Make.centerX.equalTo (Self.view.mas_centerX);
    Make.height.mas_equalTo (25);

    }]; [_imground mas_makeconstraints:^ (Masconstraintmaker *make)
        {Make.top.equalTo (_lblresult.mas_bottom). Offset (padding * 2);
    Make.centerX.equalTo (Self.view.mas_centerX);

    }]; [_btnindex mas_makeconstraints:^ (Masconstraintmaker *make)
    {Make.center.equalTo (_imground);
}];
    /** change the selection position/-(void) segmentedvaluechanged {orign = 0.0;

    _lblresult.text = @ "";
    [_btnindex.imageview.layer removeallanimations];

    [_imground.layer removeallanimations];
    _btnindex.imageview.transform = cgaffinetransformmakerotation (0);
_imground.transform = cgaffinetransformmakerotation (0);
    /** change the direction of selection/-(void) segmenteddirectionvaluechanged {ISCW = _segmenteddirection.selectedsegmentindex = 1;
[Self segmentedvaluechanged];
  /** Click event/-(void) Btnindexclick {  _btnindex.userinteractionenabled = NO;

[Self getresult]; /** simulation from server request result/-(void) GetResult {//Generate random number as Stop item//Here starting from 0 0-item_count-1 random = (arc4random ()% ITE

    M_count);

    _lblresult.text = [NSString stringwithformat:@ "Stop Item =%.0LF", Random];

    if (_segmenteditem.selectedsegmentindex = = 0) {[Self beginindexrotate];
    else {[Self beginroundrotate]; /** start rotation-pointer//(void) beginindexrotate {cabasicanimation *anim = [Cabasicanimation animationwithkeypath:@] Tran
    Sform.rotation "];
        if (ISCW) {[Anim setfromvalue:[nsnumber NUMBERWITHFLOAT:M_PI * Orign]];
        Orign = Random/item_count * 2;

    [Anim settovalue:[nsnumber Numberwithfloat:m_pi * (Rotation_extend + orign)];
        else {[Anim setfromvalue:[nsnumber NUMBERWITHFLOAT:-M_PI * Orign]];
        Orign = 2-random/item_count * 2;

    [Anim settovalue:[nsnumber Numberwithfloat:-m_pi * (Rotation_extend + orign)]; } anim.duration = anim_time;
    Anim.removedoncompletion = NO;
    Anim.fillmode = Kcafillmodeforwards;
    [Anim setdelegate:self];

    [Anim settimingfunction:[camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseout]];
[_btnindex.imageview.layer addanimation:anim forkey:@ "Rotation"]; /** start rotation-chassis//(void) beginroundrotate {cabasicanimation *anim = [cabasicanimation animationwithkeypath:@] Transf
    Orm.rotation "];
        if (ISCW) {[Anim setfromvalue:[nsnumber NUMBERWITHFLOAT:M_PI * Orign]];
        Orign = 2-random/item_count * 2;

    [Anim settovalue:[nsnumber Numberwithfloat:m_pi * (Rotation_extend + orign)];
        else {[Anim setfromvalue:[nsnumber NUMBERWITHFLOAT:-M_PI * Orign]];
        Orign = Random/item_count * 2;

    [Anim settovalue:[nsnumber Numberwithfloat:-m_pi * (Rotation_extend + orign)];
    } anim.duration = Anim_time;
    Anim.removedoncompletion = NO;
    Anim.fillmode = Kcafillmodeforwards; [Anim SetdelegatE:self];

    [Anim settimingfunction:[camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseout]];
[_imground.layer addanimation:anim forkey:@ "Rotation"];
    /** Animation start/-(void) Animationdidstart: (caanimation *) anim {_btnindex.userinteractionenabled = NO;
    _segmenteditem.userinteractionenabled = NO;
_segmenteddirection.userinteractionenabled = NO; /** Animation End */(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag {_btnindex.userinteractionenabled = Y
    ES;
    _segmenteditem.userinteractionenabled = YES;

    _segmenteddirection.userinteractionenabled = YES;
                                         if (!alert) {alert = [[Uialertview alloc]initwithtitle:nil message:@ ']
                                Delegate:nil cancelbuttontitle:@ "OK"
    Otherbuttontitles:nil, nil];
    } alert.message = [NSString stringwithformat:@ "You smoke%.0LF", random]; [Alert shOW];
 } @end

Look at the effect.

third, other existing problems:

Demo transfer disk is to get the simulation data began to rotate, so easy to control;
But in the actual application, the request results from the server, may be due to network problems such as delay, the click "Start" after a long time did not turn the problem.

Further amendments are necessary here. Four, demo download

Code download

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.