Float images randomly on screen objective c - ios

I want to implement code for moving images randomly on screen in any direction. These images are also clickable ones. I have written code for 2 clickable images, but it is always starting from left corner and ending at one position. Here is the code:
[UIView animateWithDuration:3.0
delay:0.0
options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.imageView.center = CGPointMake(self.imageView.center.x,
self.imageView.center.y+100);
}
completion:NULL];
I have figured out the clickable part. Now I want to make the image float around the screen. I have tried varying the values inside CGPointMake but getting the same results. Please help to figure me out. Thanks.

Have a look at the following options
[UIView animateWithDuration:5.0
delay:0
options:(UIViewAnimationOptionBeginFromCurrentState |
UIViewAnimationOptionCurveEaseInOut |
UIViewAnimationOptionAllowUserInteraction)
animations:^{
...
}];
} completion:^(BOOL finished) {
...
}];

Related

How to make animated counting with motion blur effect like Smartisan OS Weather App?

Like the Title.
The demo video is here :https://youtu.be/siiG-cBD4j4
Eager for your sincere help.Thank you indeed.
You can write your date changing code inside this animation block. You get the same motion blur effect:
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.8
initialSpringVelocity:0.6
options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.view.superview layoutIfNeeded];
;
} completion:nil];

UIViewAnimationOptionRepeat Completion?

I am doing a very simple repeating animation to fade a label in and out, seen below. I assumed the completion block would be called every time the animation finishes, but when using UIViewAnimationOptionRepeat it is never called. So how am I supposed to stop this animation?
I know I can use [self.lbl.layer removeAllAnimations];, however that ends its very abruptly. I want to know when it has finished a cycle of the animation so I can stop it at that time.
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{
self.lbl.alpha = 0;
} completion:^(BOOL finished){
if (finished) NSLog(#"done");
}];
If you want the animation with the UIViewAnimationOptionRepeat option to be finite, you have to set the repeat count via + (void)setAnimationRepeatCount:(float)repeatCount of UIView.
In case of block-based animation (that is your case), you should set repeat count from within animation block. So, here is your code modified:
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{
[UIView setAnimationRepeatCount:4]; // 4 as example here
self.lbl.alpha = 0;
} completion:^(BOOL finished){
if (finished) NSLog(#"done");
}];
This is mentioned in documentation of setAnimationRepeatCount: method of UIView.
maybe this kind of solution using a selector can help you:
- (void) animateTextWithMax:(NSNumber *)max current:(NSNumber *)current
{
NSLog(#"max = %d, current = %d",max.intValue, current.intValue);
textlabel.alpha = 1.0f;
[UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionAutoreverse
animations:^{
textlabel.alpha = 0.0f;
}completion:^(BOOL finished){
NSLog(#"finished");
if (current.intValue < max.intValue) {
[self performSelector:#selector(animateTextWithMax:current:) withObject:max withObject:[NSNumber numberWithInteger:(current.intValue+1)]];
}
}];
}
then you can call the animation by this way:
[self animateTextWithMax:[NSNumber numberWithInt:3] current:[NSNumber numberWithInt:0]];
Maybe this is not the best solution because you are not using the UIViewAnimationOptionRepeat option, but I think it can work.
I hope this helps you.
As you've discovered, the completion block is not called, because the animation never ends. Off the top of my head, you've got two options:
Add another animation to the label, setting it to your desired final value, with the UIViewAnimationOptionBeginFromCurrentState option. I'm not sure what effect this has on the existing, repeating animation, though. If that doesn't work...
Get the current position of your animation by examining the label.layer.presentationLayer. Stop all animations, set your value to the current state, then add a new animation to transition from the current state to the desired final state.

Stairs of animation animationWithDuration

I use animationWithDuration for animating elements. An I have 1 animation inserted in completion block of another.
[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
} completion:^(BOOL finished) {
... //and simple
}];
}];
So I have Stairs of six - nine elements of blocks of animationWithDuration.So how I can optimize this?
Sorry for my English.
If you have many nested animations the code quickly becomes very unreadable.
So the approach here is maybe to hold an array of all animation blocks and to iterate through them without nesting them.
One possible example how this looks in code is shown here:
Multiple UIView Animations Without Nested Blocks

iOS - animation not repeating

I've made something like:
[UIView animateWithDuration:2.0
delay:0
options:UIViewAnimationOptionRepeat
animations:^{
NSLog(#"Repeating...");
/** code **/
}
completion:^(BOOL finished) {
/** code **/
}];
But the animation does not repeat itself.
Am I missing something?
I've removed the color animation, but the animation doesn't repeat yet. But I must be doing something really wrong, 'cause I tried the following simple code and the animation still doesn't repeat:
[UIView animateWithDuration:1.0f
delay:0
options: UIViewAnimationOptionRepeat
animations:^{
NSLog(#"Repeating...");
self.alpha = 1;
}
completion:nil];
Depending on the animation (still would like to see your code) you might have to reverse your animation in order for it to repeat. For example, if your animation changes an alpha value from 0 to 1, repeating that, without reversing the alpha back to 0, isn't going to do much. You may need to add UIViewAnimationOptionAutoreverse to your animation options.

iPhone animation options

I am moving a UIImageView with the following animation:
[UIView animateWithDuration:.5 delay:0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations:^{
myImage.frame = CGRectOffset(myImage.frame, myImageOffset.x, myImageOffset.y);
}
completion:^(BOOL finished){
}
];
The problem is the image starts moving slow, then speeds up and slows down again before ending the animation. Isn't the UIViewAnimationCurveLinear option supposed to make the animation perform at a constant rate?
Try using this option instead - UIViewAnimationOptionCurveLinear. I found it while looking at this question on another site - http://www.iphonedevsdk.com/forum/iphone-sdk-development/89009-block-animation-ignores-uiviewanimationcurvelinear.html

Resources