Why are these UIImages glitching when they reload on the screen?: - ios

In my code, after the frog jumps on the lily pad, the lilypad fades out slowly, (making it look like they are submerged) and then after they pass the bottom of the screen, they reload on the top of the screen. The code is as follows:
if (CGRectIntersectsRect(FrogSquare.frame, lilypadTS.frame) && (upMovement <= -1) && (swim == YES) && (startingice.hidden == NO)){
[self bounce];
[self iceblockfall];
if (lilypadused == NO) {
addedScore = 1;
lilypadused = YES;
}
//hide pad
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
[startingice setAlpha:0];
[UIView commitAnimations];
[self performSelector:#selector(hidePad) withObject:self afterDelay:4.0];
}
That part works fine, the lily pad does what its supposed to do. The following is where I get this weird glitch:
if (lilypad.center.y > 610) {
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
lilypad.center = CGPointMake(RandomPosition, -22);
lilypadused = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[lilypad setAlpha:1];
[UIView commitAnimations];
[self performSelector:#selector(showlilypad) withObject:self afterDelay:0];
} else if ((lilypad.center.y > 610) && (lilypad.hidden == YES)) {
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
lilypad.center = CGPointMake(RandomPosition, -22);
lilypadused = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[lilypad setAlpha:1];
[UIView commitAnimations];
[self performSelector:#selector(showlilypad) withObject:self afterDelay:0];
}
What happens is for some reason, the lilypads will reload at the top of the screen like they're supposed to, but then as the user is jumping on lilypads below the ones at the top. If thats confusing -- Screen Dimensions 320 x 568, lilypad1 reloads at the top of the screen point = -22, then slowly falls down, so say point is 10 after falling for a bit, say the player jumps on lilypad2 that HAS NOT been hidden and gone past 610 (to reload at the top), it makes lilypad1 hide instantly, why is this??

The amount of code you've provided really isn't enough to diagnose your problem. You are likely (definitely) referencing the incorrect lilypad which is causing the errant behavior. You should probably take a close look at the architecture of your app to make sure you are properly managing the list of lilypads in the best way.
Also, whenever you use delays - note that those delays are approximate, not exact. It is possible that you are in a race condition, where the hide has yet to occur but you're already showing it again at the top, then the hide triggers later.
Lastly, if afterDelay is 0 you might as well just call the showlilypad method directly - no need to really delay for 0 seconds.
Good luck!

Related

How to constantly rotate a button by 45 degrees when another button is tapped?

I'm using this code:
[UIView animateWithDuration:ANIMATION_DURATION_SLOW
delay:0
usingSpringWithDamping:0.4
initialSpringVelocity:5.0
options:0
animations:^{
_button.transform = CGAffineTransformMakeRotation(-M_PI_4);
}
completion:nil];
But, the button only rotates the first time. No matter how many times i call this method it doesn't rotate again.
Setting a rotation transform doesn't add to the previous transform, it replaces it so you need to remember where it was during the last tap. A quick way to do this is a static variable.
static tapCount = 0;
tapCount++;
[UIView animateWithDuration:ANIMATION_DURATION_SLOW
delay:0
usingSpringWithDamping:0.4
initialSpringVelocity:5.0
options:0
animations:^{
_button.transform = CGAffineTransformMakeRotation(-M_PI_4 * tapCount);
}
completion:nil];
It's because you need to change the angle.
consider:
-(CGFloat)angle{
if (!_angle) {
_angle = -M_PI_4;
}
_angle += -M_PI_4;
return _angle;
}

UIView Core Animation not working in viewdidLoad method?

I am new in IOS dev, using core animation in my project. i want my UIVIew default outside the main view and animate them on condition. when i call it in button method it work perfectly fine but when i call it in viewdidload nothing happen when program start.
Secondly i want to do this animation many times in my project Like this: 1 animate from right (Outside the screen) to left (Somewhere on screen), then 1 and 2 below them, then 1 2 and 3 and finally 1 2 3 and 4. can any one guid me how to do this? plz help
//1
//1 2
//1 2 3
//1 2 3 4
- (void)viewDidLoad
{
[super viewDidLoad];
[self animate];
}
-(IBAction)move:(id)sender
{
[self animate];
}
-(void) animate {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos;
pos.x = 241.0f;
pos.y = 20.0f;
mover.center = pos;
[UIView commitAnimations];
}
Please try to call [self animate]; this method on 1 second delay of view did load.
try this...
dispatch_async(dispatch_get_main_queue(), ^{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos;
pos.x = 241.0f;
pos.y = 20.0f;
mover.center = pos;
[UIView commitAnimations];
});

Trying to perform multiple animations at once

Seems strange that i could not find any answer for this all over the net, but seems that if you want to move 6 UIViews at the same time in a different speed ,you can't do that.
If i am using one of this 2 example, i get that sometimes only some of the views are moving, and sometimes all of them (as expected) .
There is no way to move 6-7-8 UIViews at the same time ,with different duration?
1.
for(UIButton *button in buttons)
{
float r= arc4random()%10;
float t =0.1+ 1.0/r;
[UIView beginAnimations:#"Anim0" context:nil];
[UIView setAnimationDuration:t];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:button cache:YES];
CGRect newframe=button.frame;
newframe.origin.y=0;
button.frame=newframe;
[UIView commitAnimations];
}
2.
for(UIButton *button in buttons)
{
int random=arc4random()%10;
float time=0.5+ 1/(float)random;
CGRect newframe=button.frame;
newframe.origin.y=0;
[UIView transitionWithView:button
duration:time
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
button.frame=newframe;
}
completion:nil];
}
You need to limit the random number generator.
Your function...
arc4random() % 10;
Will sometimes return 0.
Then passing this into...
float time = 0.5 + 1/random;
Will make time = inf.
You can see this by logging out the time values.
A time of inf will make the button not animate.
Of course it is possible. The problem is that you're sending the wrong message. Based on your second snippet, this is an example of how it could be done:
for (UIButton *button in self.buttons) {
int random = (arc4random() % 10) + 1; // "+1" Added later in the answer, see Fogmeister's answer
float time = 0.5 + 1 / (float)random;
CGRect newframe = button.frame;
newframe.origin.y = 0;
[UIView animateWithDuration:time
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
button.frame = newframe;
} completion:^(BOOL finished) {
// The animation have finished
}];
}

How to create animation for UIview that needs to pull down to up?

I want to create a screen to pull up from down and need to pull down.like iOS notifications screen (but in reverse direction).Please share if any example is there.Thanks in advance.
The full-on way to do this in iOS 7 is with an interactive custom transition. You will use a UIScreenEdgePanGestureRecognizer and respond to the drag gesture from the edge of the screen by performing part of the transition animation to match the distance the user's finger has travelled. Look into the UIPercentDrivenInteractiveTransition class to help you do that.
Here's a complete example you can download and run: it happens that I'm doing it from the left or right, not the top or bottom, but it is the same basic idea.
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p296customAnimation2/ch19p620customAnimation1/AppDelegate.m
But beware: if you try to do it from the top or the bottom you will get interference from the notification center (top) and the control center (bottom). It can be tricky to prevent that interference.
Hi you can use it.
- (void)showShareView {
[self.view bringSubviewToFront:self.view_Email];
[UIView beginAnimations:#"Show"
context:nil];
[UIView setAnimationDuration:0.3F];
CGRect frame = self.view_Email.frame;
if (IS_IPHONE5) {
if(txtFieldActiveFlag==1){
frame.origin.y = 568-420;
}else{
frame.origin.y = 568-220;
}
} else {
frame.origin.y = 480 - 275 - 88;
}
self.view_Email.frame = frame;
[UIView commitAnimations];
}
- (void)hideShareView {
[UIView beginAnimations:#"Show"
context:nil];
[UIView setAnimationDuration:0.3F];
CGRect frame = self.view_Email.frame;
if (IS_IPHONE5) {
if(txtFieldActiveFlag==2){
frame.origin.y = 568-220;
}else{
frame.origin.y = 568;
}
} else {
frame.origin.y = 480;
}
self.view_Email.frame = frame;
[UIView commitAnimations];
}
//call method from any button action.
[self showShareView]; //show the view up
[self hideShareView]; //show the view down
Thanks

How to lengthen the UISearchBar in x direction with animation?

I want to lengthen the right search bar 20 px to the left, and shorten left search bar 20 px to the right with animation. How can I do that?
CGRect leftFrame = self.leftSearchBar.frame;
//leftFrame.origin.x = leftFrame.origin.x - 20;
leftFrame.size.width = leftFrame.size.width - 40;
CGRect rightFrame = self.rightSearchBar.frame;
rightFrame.origin.x = rightFrame.origin.x - 40;
rightFrame.size.width = rightFrame.size.width + 40;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.leftSearchBar.frame = leftFrame;
self.rightSearchBar.frame = rightFrame;
[UIView commitAnimations];
It doesn't work. It first resizes views without animation, then moves the right view with animation.
Assuming you've not changed the anchor points, or anything like that, simply expanding the width will make it stretch to the right. Wrap this within a UIView animation block, and it should animate according to the specification within the question, as so:
UISearchBar* searchbar = ...; // However you've created your UISearchBar, we'll refer to it as 'searchbar'
CGRect searchFrame = searchbar.frame;
[UIView animateWithDuration:0.8
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
searchFrame.size.width += 30;
searchbar.frame = searchFrame;
}
completion:nil];
You'll need to make sure the += 30 doesn't get called more than once, as otherwise you'll be expanding by 30 points each time this is called. If you know the size before hand, you could simply substitute it with = size + 30, which is a safer bet incase you call the animation more than once.

Resources