Some problems with View animation - objective-c

I'm trying create animation on my View. Now I have any trouble. Animation is work only with _pageViewController
_pageViewController.view.frame = frame;
_pageViewController.view.alpha = alpha;
but _bottomImage.frame = frame1; doesn't work. I don't understand Why?
If I comment this lines
//_pageViewController.view.frame = frame;
//_pageViewController.view.alpha = alpha;
_bottomImage.frame = frame1 begin work.
What am I doing wrong?
- (IBAction)share:(id)sender {
CGRect frame =self.pageViewController.view.frame;
CGRect frame1 = _bottomImage.frame;
float alpha = 1.0;
if (frame.origin.y==65.0) {
frame.origin.y = 240;
frame1.origin.y = 650;
alpha = 0.5;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.5];
[UIView setAnimationDelay:0.1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
_pageViewController.view.frame = frame;
_pageViewController.view.alpha = alpha;
_bottomImage.frame = frame1;
[UIView commitAnimations];
}else{
[self closeShare];
}
}

Related

Keep the End Position of Animation

I tried everything, but everytime its the same problem:
i want to move a button for y = 276. when the animation ends, my buttons jumps back to the startposition, but i want that the endanimationposition is the new position, and the button has to stay there, until a new animation is called.
tried with
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
CGPoint p = _cell00.center;
p.x += 100;
_cell00.center = p;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
same issue like
[UIView animateWithDuration:1.0 animations:^{
_cell00.frame = CGRectOffset(_cell00.frame, 0, 20);
}];
or
[UIView animateWithDuration:0.5 animations:^{
_cell00.center = CGPointMake(0, 20.0);
}];
EDIT:
I call this Animation with my Button called statisticsBUttonPressed.
- (IBAction)statisticsButtonPressed:(id)sender {
if (statOrGame == 0) {
statOrGame = 1;
}else {
statOrGame = 0;
}
NSLog(#"statOrGame? %d", statOrGame);
[self animateView:(UIButton *)sender];
}
-(void) animateView:(UIButton *)sender {
sender.userInteractionEnabled = NO;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{ // The iOS device = iPhone or iPod Touch
NSLog(#"IS IPHONE 5");
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 568)
{
if (statOrGame) {
[UIView animateWithDuration:1.0 animations:^{
_cell00.frame = CGRectOffset(_cell00.frame, 0, 20);
}];
}
}
}
}
The problem is that you're using auto layout. You can't (just) change the frame of a view that is positioned by auto layout, because what positions the view is its constraints. You need to change the constraints. You can do this at the end of the animation, or you can simply animate the change of constraints.

How can I fade a UIView in while moving it up?

I have a UIView and I want to give it an effect that causes it to start with an alpha of 0 and then while moving up 50 pixels animated it also changes to an alpha of 1. How can this be done in Objective-C?
[UIView animateWithDuration:1.0 animations:^{
//here change position and alpha
CGRect newFrame = view.frame;
newFrame.origin.y += 50;
view.frame = newFrame;
view.alpha = 1.0;
} completion:^(BOOL finished) {
//called when animation did finish. do what you want
}];
https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html
Everything about animations.
It's all right there.
//something similar to what you want:
- (IBAction)showHideView:(id)sender
{
// Fade out the view right away
[UIView animateWithDuration:1.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
thirdView.alpha = 0.0;
}
completion:^(BOOL finished){
// Wait one second and then fade in the view
[UIView animateWithDuration:1.0
delay: 1.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
thirdView.alpha = 1.0;
}
completion:nil];
}];
}
Something like this would do the trick:
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(self.view.center.x, self.view.center.y, 100, 50)];
box.backgroundColor = [UIColor blackColor];
box.alpha = 0.0f;
[self.view addSubview:box];
[UIView animateWithDuration:2.0f
animations:^{
box.alpha = 1.0f;
CGRect frame = CGRectMake(self.view.center.x, self.view.center.y - 100, 100, 50);
[box setFrame:frame];
}];

Transformation unable to perform twice

I have this function,
-(void)transitionstar{
star.hidden = NO;
star2.hidden = NO;
star3.hidden = NO;
star4.hidden = NO;
star5.hidden = NO;
star6.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4f];
[UIView animateWithDuration:0.0 animations:^{
CGAffineTransform scale = CGAffineTransformMakeScale(1, 1);
CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform rotate2 = CGAffineTransformMakeRotation(-360.0);
CGAffineTransform rotate3 = CGAffineTransformMakeRotation(-720.0);
CGAffineTransform rotate4 = CGAffineTransformMakeRotation(720.0);
CGAffineTransform rotate5 = CGAffineTransformMakeRotation(1080.0);
CGAffineTransform rotate6 = CGAffineTransformMakeRotation(-1080.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform translate2 = CGAffineTransformMakeTranslation(600, -600);
CGAffineTransform translate3 = CGAffineTransformMakeTranslation(400, 400);
CGAffineTransform translate4 = CGAffineTransformMakeTranslation(-200, 200);
CGAffineTransform translate5 = CGAffineTransformMakeTranslation(900, -300);
CGAffineTransform translate6 = CGAffineTransformMakeTranslation(-200, 500);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
CGAffineTransform transform2 = CGAffineTransformConcat(translate2, scale);
transform2 = CGAffineTransformConcat(transform2, rotate2);
CGAffineTransform transform3 = CGAffineTransformConcat(translate3, scale);
transform3 = CGAffineTransformConcat(transform3, rotate3);
CGAffineTransform transform4 = CGAffineTransformConcat(translate4, scale);
transform4 = CGAffineTransformConcat(transform4, rotate4);
CGAffineTransform transform5 = CGAffineTransformConcat(translate5, scale);
transform5 = CGAffineTransformConcat(transform5, rotate5);
CGAffineTransform transform6 = CGAffineTransformConcat(translate6, scale);
transform6 = CGAffineTransformConcat(transform6, rotate6);
star.transform = transform;
star2.transform = transform2;
star3.transform = transform3;
star4.transform = transform4;
star5.transform = transform5;
star6.transform = transform6;
}
completion:^(BOOL finished){
if (finished) {
star.hidden = YES;
star2.hidden = YES;
star3.hidden = YES;
star4.hidden = YES;
star5.hidden = YES;
star6.hidden = YES;
}
}];
[UIView commitAnimations];
}
When i call it first time, it did worked.
However, i call again within same view, it cannot perform and stuck there.
-hidden work
-transition not work
-rotation not work
-nslog work
Why second time will become like this?
Update
if(!positionrepeat)
{
//Display Correct IMAGE;
[isrepeat addObject:[NSNumber numberWithInt:positionvalue]];
//soundeffect = [self createSoundID: #"correct"];
//AudioServicesPlaySystemSound(soundeffect);
[self displayresulttext:#"correct"];
[self.view setNeedsDisplay];
[self transitionstar];
correct++;
completed.text = [NSString stringWithFormat:#"%d", correct];
[self result];
}
This is how I call the function. However, it still same.
in this bit of code your changed the transform
star.transform = transform;
star2.transform = transform2;
star3.transform = transform3;
star4.transform = transform4;
star5.transform = transform5;
star6.transform = transform6;
for eg consider your having the star1 initial transform is x ,
now your doing some calculation for transform....
CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
then
your changed the transform of star x to y...
star.transform = transform;//say this is y
now your star transform will become y okay...
again doing this code by call again so again your initial star transform is y and your equating transform to y so no need and u didn't see any of the result...
y=y so no result....
So need to change some logic like this or some what you want but this is an idea...
1st change: you need to store the transform of each star so you need some set of variables of type transform...
CGAffineTransform star1Initial,star2Initial,star3Initial,star4Initial,star5Initial,star6Initial;
2nd change:you need to save the initial transform of stars in the view did load....
star1Initial=star1.transform;
star2Initial=star2.transform;
star3Initial=star3.transform;
star4Initial=star4.transform;
star5Initial=star5.transform;
star6Initial=star6.transform;
-(void)transitionstar{
star1.hidden = NO;
star2.hidden = NO;
star3.hidden = NO;
star4.hidden = NO;
star5.hidden = NO;
star6.hidden = NO;
star1.transform=star1Initial;
star2.transform=star2Initial;
star3.transform=star3Initial;
star4.transform=star4Initial;
star5.transform=star5Initial;
star6.transform=star6Initial;
// [UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.4f];
[UIView animateWithDuration:1.0 animations:^{
CGAffineTransform scale = CGAffineTransformMakeScale(1, 1);
CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform rotate2 = CGAffineTransformMakeRotation(-360.0);
CGAffineTransform rotate3 = CGAffineTransformMakeRotation(-720.0);
CGAffineTransform rotate4 = CGAffineTransformMakeRotation(720.0);
CGAffineTransform rotate5 = CGAffineTransformMakeRotation(1080.0);
CGAffineTransform rotate6 = CGAffineTransformMakeRotation(-1080.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform translate2 = CGAffineTransformMakeTranslation(600, -600);
CGAffineTransform translate3 = CGAffineTransformMakeTranslation(400, 400);
CGAffineTransform translate4 = CGAffineTransformMakeTranslation(-200, 200);
CGAffineTransform translate5 = CGAffineTransformMakeTranslation(900, -300);
CGAffineTransform translate6 = CGAffineTransformMakeTranslation(-200, 500);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
CGAffineTransform transform2 = CGAffineTransformConcat(translate2, scale);
transform2 = CGAffineTransformConcat(transform2, rotate2);
CGAffineTransform transform3 = CGAffineTransformConcat(translate3, scale);
transform3 = CGAffineTransformConcat(transform3, rotate3);
CGAffineTransform transform4 = CGAffineTransformConcat(translate4, scale);
transform4 = CGAffineTransformConcat(transform4, rotate4);
CGAffineTransform transform5 = CGAffineTransformConcat(translate5, scale);
transform5 = CGAffineTransformConcat(transform5, rotate5);
CGAffineTransform transform6 = CGAffineTransformConcat(translate6, scale);
transform6 = CGAffineTransformConcat(transform6, rotate6);
star1.transform = transform;
star2.transform = transform2;
star3.transform = transform3;
star4.transform = transform4;
star5.transform = transform5;
star6.transform = transform6;
}
completion:^(BOOL finished){
if (finished) {
star1.hidden = YES;
star2.hidden = YES;
star3.hidden = YES;
star4.hidden = YES;
star5.hidden = YES;
star6.hidden = YES;
}
}];
// [UIView commitAnimations];
}
i hope this will help you....
Hi try this code for spin animation.....
- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
CABasicAnimation* rotationAnimation;
int rotations=1;
rotationAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
I am assuming that the method you wrote above is working within a class that is derived from UIView.
If this assumption is correct, then your method may be called when the view has to be redrawn.
To redraw your view, you have to call the setNeedsDisplay method, as such:
[myCustomView setNeedsDisplay];

Using Block Completion Handler in iOS 4 for animation

I would like to animate my subviews movement when rotating the device, changing the alpha to 0, move the view to the new position and reset the alpha to 1.
Using this code in didRotateFromInterfaceOrientation causes the view to flash and fade very quickly and then reappear. I would like to avoid this behaviour.
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 0.0;
CGRect newFrame = anObject.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
anObject.frame = newFrame;
} completion:^ (BOOL finished){
if (finished) {
anObject.alpha = 1.0;
}
}];
Is there a way around this flashing?
Thanks
Maybe actually animate alpha on completition ? rather than flash it ? :)
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 0.0;
CGRect newFrame = anObject.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
anObject.frame = newFrame;
} completion:^ (BOOL finished){
if (finished) {
[UIView animateWithDuration:kAnimationDuration
animations:^{
anObject.alpha = 1;}
}
}];
Cheers,
Krzysztof Zabłocki

iPhone SDK: TextView, Keyboard in Landscape mode

How do I make sure that the textview is shown and the keyboard is not obscuring the textview, while in landscape.
Using UICatalog I created a TextViewController which works. In it there are two methods for calling the keyboard and making sure that textView is positioned above the keyboard. his just works great in Portrait mode.
I got the Landscape mode working, but on the textView is still being put to the top of the iPhone to compensate for the keyboard in portrait mode.
I changed the methods for showing the keyboards.
Below is the code for this methods: (I will just let see the code for show, since the hide code will be the reverse..
- (void)keyboardWillShow:(NSNotification *)aNotification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
} else if (orientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"Left"); // Verijderen later
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.width -= keyboardRect.size.height;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
} else if (orientation == UIInterfaceOrientationLandscapeRight){
NSLog(#"Right"); // verwijderen later.
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.width -= keyboardRect.size.width;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
}
I know that I have to change the line frame.size.height -= keyboardRect.size.height but I do not seem to get it working.
I tried frame.size.width -= keyboardRect.size.height that did not work. Losing the keyboardRect and frame all together work, however off course the keyboard obscures the textview........
I found the above code wouldn't work when in Landscape Mode on iPad
Note: I am moving all Fields, as in my case that's what i needed :-)
- (void)keyboardWillShow:(NSNotification*)aNotification {
// Only do for Landscape Mode
if (UIInterfaceOrientationIsLandscape([self interfaceOrientation])){
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.x -= keyboardSize.height-44;
frame.origin.y -= keyboardSize.height-44;
frame.size.height += keyboardSize.height-44;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = YES;
}
keyboardInUse = YES;
}
- (void)keyboardWillHide:(NSNotification*)aNotification {
if (UIInterfaceOrientationIsLandscape([self interfaceOrientation])){
if (viewMoved) {
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-44;
frame.origin.x += keyboardSize.height-44;
frame.size.height -= keyboardSize.height-44;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = NO;
}
}
keyboardInUse = NO;
}
If you think you need different code for the different orientations, you're doing something else wrong. Once the orientation has changed and your superview has responded, the value that needs changing should always be the frame's height.
Here is a code that I use for this purpose. It works on both iPad and iPhone is landscape and portrait modes (inspired by a code from Apple documentation):
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float offset;
if UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
offset = kbSize.height;
else
offset = kbSize.width;
[UIView animateWithDuration:0.5
animations:^{
CGRect frameTxtField = myTextField.frame;
frameTxtField.origin.y -= offset;
myTextField.frame = frameTxtField;
}
];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float offset;
if UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
offset = kbSize.height;
else
offset = kbSize.width;
[UIView animateWithDuration:0.5
animations:^{
CGRect frameTxtField = myTextField.frame;
frameTxtField.origin.y += offset;
myTextField.frame = frameTxtField;
}
];
}
Don't forget to call the registerForKeyboardNotifications method (e.g., in viewDidLoad).