objective c - iOS Rotation Gesture after animation -
first question take easy!
creating simple ios app. have view contains rotating dial (a uiimageview) (rotates through rotation gesture) , uibutton when pressed uses uiview animatewithduration move button 1 of 3 locations.
my issue.. rotating dial fine, moving button fine, but.. when move button, rotate dial, location of button "reset" original frame.
how can stop rotation effecting location of button / movement of button.
viewcontroller.h
#import <uikit/uikit.h> #import <avfoundation/avfoundation.h> @interface viewcontroller : uiviewcontroller <uigesturerecognizerdelegate>{ iboutlet uiimageview *lock; iboutlet uilabel *number; iboutlet uilabel *displaynumber1; iboutlet uilabel *displaynumber2; iboutlet uilabel *displaynumber3; iboutlet uibutton *slider; avaudioplayer *theaudio; } @property (retain, nonatomic) iboutlet uiimageview *lock; @property (retain, nonatomic) iboutlet uilabel *number; @property (retain, nonatomic) iboutlet uilabel *displaynumber1; @property (retain, nonatomic) iboutlet uilabel *displaynumber2; @property (retain, nonatomic) iboutlet uilabel *displaynumber3; @property (retain, nonatomic) iboutlet uibutton *slider; @property (retain, nonatomic) avaudioplayer *theaudio; -(ibaction)moveslider:(id)sender; @end
viewcontroller.m
#import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller #define radians_to_degrees(radians) ((radians) * (180.0 / m_pi)) @synthesize lock, number, slider, displaynumber1, displaynumber2, displaynumber3, theaudio; cgfloat _lastrotation; int lastnumber; nstimer *clicktimer; int _whichnumberselected; int _currentdirection; cgfloat _changedirectionmatch; float no1left = 61; float no2left = 151; float no3left = 238; nsstring *lasttext1; cgrect newframeset; - (bool)prefersstatusbarhidden { return yes; } - (void)viewdidload { [super viewdidload]; lock.userinteractionenabled = yes; uirotationgesturerecognizer *rotationgesturerecognizer = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@selector(handlerotationwithgesturerecognizer:)]; rotationgesturerecognizer.delegate = self; [lock addgesturerecognizer:rotationgesturerecognizer]; _lastrotation = 0; lastnumber = 0; nsstring *path = [[nsbundle mainbundle] pathforresource:@"click" oftype:@"wav"]; nsurl *fileurl = [[nsurl alloc] initfileurlwithpath: path]; theaudio = [[avaudioplayer alloc] initwithcontentsofurl:fileurl error:null]; theaudio.volume = 1.0; [theaudio preparetoplay]; _currentdirection = 0; _changedirectionmatch = 0; _whichnumberselected = 1; lasttext1 = ([nsstring stringwithformat:@"0"]); } -(ibaction)moveslider:(id)sender { [uiview animatewithduration:0.5 delay:0 options:uiviewanimationoptioncurveeaseout animations:^{ cgrect newframe = slider.frame; switch (_whichnumberselected) { case 1: newframe.origin.x= no2left; _whichnumberselected = 2; break; case 2: newframe.origin.x= no3left; _whichnumberselected = 3; break; case 3: newframe.origin.x= no2left; _whichnumberselected = 4; break; case 4: newframe.origin.x= no1left; _whichnumberselected = 1; break; default: break; } slider.frame = newframe; newframeset = newframe; } completion:nil]; } -(void)handlerotationwithgesturerecognizer:(uirotationgesturerecognizer *)rotationgesturerecognizer{ cgfloat newrot = radians_to_degrees(rotationgesturerecognizer.rotation) / 3.6; lock.transform = cgaffinetransformrotate(lock.transform, rotationgesturerecognizer.rotation); cgfloat c = _lastrotation - newrot; _lastrotation = c; if(c < 0){ c = c + 100; _lastrotation = c; } if(c >= 100){ c = c-100; _lastrotation = c; } if(c >= 99.5){ displaynumber1.text = @"0"; } else { displaynumber1.text = ([nsstring stringwithformat:@"%.f", c]); } if([displaynumber1.text isequaltostring:lasttext1]){ } else { [theaudio play]; } lasttext1 = displaynumber1.text; rotationgesturerecognizer.rotation = 0.0; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
welcome stack overflow.
my guess view controller has autolayout active. if that's case, need change animation code animate button manipulating 1 or more constraints on rather changing it's frame.
the problem when change frame directly, @ point triggers re-layout of form, , constraints snap place. if don't explicitly add constraints, system does.
the rotation animation should fine since changing rotation on view's transform, not it's position, , there no constraints i'm aware of rotation.
what animate using constraints attach constraints button in ib, control-drag constraints view controller's header create outlets constraints. (leading , top edge constraints, say.)
then in animation change constant(s) on constraint(s) , call layoutifneeded.
the alternative turn off auto-layout view controller, use old style "struts , springs" layout, , frame animation work always.
Comments
Post a Comment