ios animation, rotating button, button suddenly changes position - ios

I am trying to implement animations in my application. I just want to try a simple rotation of a button. The button itself is rotated, but also it changes its position and the rotation angle is different than the one I want. I have also added the autoresizesSubwievs = NO;
- (IBAction)rotateView:(id)sender
{
[UIView animateWithDuration:0.6f delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.view.autoresizesSubviews = NO;
[self.buttoner setTransform:CGAffineTransformRotate(self.buttoner.transform, 90.0f)];
} completion:nil];
}
What am I doing wrong? Can you please help me?
EDIT:
I have two buttons, when the first gets pressed, I call the above method ... but this causes to move the first button and then to move the second button and rotate it with inappropriate angle. I dont understand that at all..

For the angle problem, Instead of passing 90, you should pass the radian value of the angle which is M_PI/2 .
[self.buttoner setTransform:CGAffineTransformRotate(self.buttoner.transform, M_PI/2)];

Related

Resetting UIWebView Frame kills 75% of touchable area

I've got a webpage that is being viewed with a UIWebView. It's got an input form, so I'm shifting the UIWebView up when the keyboard is summoned, and then moving it back down when the keyboard is dismissed, so that the selected input field is centered. Code below:
-(void)keyboardWillShow:(NSNotification*) aNotification {
float offset = [self calculateYOffsetForKeyboard];
[UIView animateWithDuration:0.3f delay:0.05f options:UIViewAnimationOptionBeginFromCurrentState animations:^ {
self.web.frame = CGRectMake(webView_rect_x, offset, self.web.scrollView.frame.size.width, self.web.scrollView.frame.size.height);
} completion:nil];
}
-(void)keyboardWillHide: (NSNotification*) aNotification {
[UIView animateWithDuration:0.3f delay:0.05f options:UIViewAnimationOptionBeginFromCurrentState animations:^ {
self.web.frame = CGRectMake(webView_rect_x, 0, self.web.scrollView.frame.size.width, self.web.scrollView.frame.size.height);
} completion:nil];
}
Here's the weird part: when I do that (tap an input field to summon the keyboard then tap "Done" to dismiss it), everything but the top-left quarter of the UIWebView becomes untouchable. It looks fine, animates up and down perfectly, displays the content properly, but anything right of or below the center point doesn't respond to touches (i.e. several buttons can't be pressed, etc).
Any idea why this might happen? I've commented out lines and determined that it is the "self.web.frame" that is causing the problem: remove that movement and it works fine. I've also NSLog'd the "self.web.scrollView.frame.size.width" and "self.web.scrollView.frame.size.height" values and they seem to be changing as expected and returning to normal. Also the calculateYOffsetForKeyboard method is returning a proper value, (it changes based on which field is selected, but it's generally around -108.5).
So why would moving the frame back and forth alter the view's ability to respond to touches? Any ideas?

Move and Rotate UIButtons

I have one UIbutton. I want it to move from point A to point B. While going, that button have to do one full 360' rotation.
After some event, I want that UIButton to come back to its position, this time rotating in the reverse direction.
I wrote this code, but it didnt work. I'm seeing the button rotate in unexpected ways. What am I missing here?
To move:
[UIView animateWithDuration:0.3 animations:^{
CGAffineTransform rotate360AntiClockWise = CGAffineTransformMakeRotation(M_PI_2);
CGRect initialFrame = button.frame;
[button setTransform:rotate360AntiClockWise];
[button setFrame:CGRectMake(initialFrame.origin.x - 140.0, initialFrame.origin.y, initialFrame.size.width, initialFrame.size.height)];
}];
To comeback to the same position
[UIView animateWithDuration:0.4 animations:^{
CGAffineTransform rotate360ClockWise = CGAffineTransformMakeRotation(-M_PI_2);
[button setFrame:originalFrame]; //for movement
[button setTransform:rotate360ClockWise];
}];
}
I can't try it at the moment, but if I remember correctly using the frame causes problems when applying transformations. Have you tried setting the center of the button?
[button setCenter:newCenter]
Where newCenter is a corresponding CGPoint.
The simplest way to do a rotation and a transform without messing up the Transformation matrix of the view is:
place the button inside of a view (moveView) with the same frame-size with origin 0|0
when you want to do a rotation, rotate the button
when you want to move the button, translate the moveView
Please do this in 3 steps
CGAffineTransform matOne = CGAffineTransformMakeTranslation(x_offset, y_offset);
CGAffineTransform matTwo = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform matResult = CGAffineTransformConcat(matOne, matTwo);
// Finally
[button setTransform:matResult];
I hope this helps you. Please make sure Concat has matOne as the first parameter.
Also this is a possible duplicate for another question:
translation after rotation view using CGAffine

How to add delete effect like iphone application?

I have to give a delete thumbnail animation in my ipad application just like iphone/ipad applications delete effect.
Any body help me please sample photo is attached
If you need more details then kindly mention it in comments
I have done this using CGAffineTransformMakeRotation. Don't know there is some other better method. But What I have done is my logic you can copy that as it is and you just need to add a delete button on the left top of that view. In the following code I am just animating the thumbnail or any view just like iPad does on its home screen. One thing, You need to declare int direction globally. and every time when you will call this method you will set direction = 1;
-(void)shakeToDelete:(UIView *)shakeMe
{
[UIView animateWithDuration:0.1 animations:^
{
shakeMe.transform = CGAffineTransformMakeRotation(0.05 * direction);
}
completion:^(BOOL finished)
{
direction = direction * -1;
[self shakeToDelete:shakeMe];
}];
}
/// edit
I tried this way and got it working in my sample screen as attached in photo
You better should use an autoreverse and looped animation, cause creating animations over and over will fulfill the phone memory.
With this code, only one animation is retained.
view.transform = CGAffineTransformMakeRotation(-kDeleteAnimationAmplitude);
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
view.transform = CGAffineTransformMakeRotation(kDeleteAnimationAmplitude);
} completion:nil];
Then if you want to stop your animation, just call:
[view.layer removeAllAnimations];

CGAffineTransformRotate shifts object before performing rotation

I am trying to animate a transform of a UIButton with CGAffineTransformRotate, and while it is performing the animation properly, it shifts the button about 15 pixels down and to the left before doing it. Here's the code performing the animated transformation:
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
self.addCloseButton.transform = CGAffineTransformRotate(self.addCloseButton.transform, degreesToRadians(45));
}
completion:nil];
When I reverse the transformation it does the same thing except it shifts it back to its original position before animating (15 pixels up and 15 pixels to the right), and I do that with this code:
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
self.addCloseButton.transform = CGAffineTransformIdentity;
}
completion:nil];
Why would this shift occur? The button was created using interface builder, and the shift happens immediately even if I set the animation duration higher or add a delay.
I figured it out: turns out having "Use Autolayout" selected on my xib (which adds a bunch of auto constraints) messes things up when trying to use transforms. Turning it off fixed my problem.
It is posible to fix this while still using auto layout and storyboards. See my answer on this question: https://stackoverflow.com/a/19582959

Issue rotating a UIView object using Core Animation and blocks

I have a problem with a UIView-subclass object that I am rotating using Core Animation in response to a UISwipeGesture.
To describe the context: I have a round dial that I have drawn in CG and added to the main view as a subview.
In response to swipe gestures I am instructing it to rotate 15 degrees in either direction dependent on whether it;s a left or right swipe.
The problem that it will only rotate each way once. Subsequent gestures are recognised (evident from other actions that are triggered) but the animation does not repeat. I can go left once then right once. But trying to go in either direction multiple times doesn't work. Here's the relevant code, let me know your thoughts...
- (IBAction)handleLeftSwipe:(UISwipeGestureRecognizer *)sender
{
if ([control1 pointInside:[sender locationInView:control1] withEvent:nil])
{
//updates the display value
testDisplay.displayValue = testDisplay.displayValue + 0.1;
[testDisplay setNeedsDisplay];
//rotates the dial
[UIView animateWithDuration:0.25 animations:^{
CGAffineTransform xform = CGAffineTransformMakeRotation(radians(+15));
control1.transform = xform;
[control1 setNeedsDisplay];
}];
}
CGAffineTransform xform = CGAffineTransformMakeRotation(radians(+15));
Do you keep a total of how far the rotation is. CGAffineTransformMakeRotation are not additive. Only the most recent is used. So you are setting it to 15 each time, not 15 more each time.
Here's a super simple example of rotating a view cumulatively. This rotates the view by 180 degrees each button press.
- (IBAction) onRotateMyView: (id) sender
{
[UIView animateWithDuration:0.3 animations:^{
myView.transform = CGAffineTransformMakeRotation(M_PI/2*rotationCounter);
} completion:^(BOOL finished){
//No nothing
}];
++rotationCounter;
}

Resources