Function sleep when animation - ios

I'm using [UIView transitionWithView:] for animation.
This method don't have delay only duration, but I need to have some delay.
For example: Need to change 1 image to another with duration 1 sec, then after 4 second change image to another with duration 1 second.
I know about [UIView animateWithDuration:] that have delay but i don't change frame it don't change image for more then 1 time.
I use next:
[UIView transitionWithView:self.view
duration: 1.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
//here change image
} completion:^(BOOL finished){
[NSThread sleepForTimeInterval: 4.0f];
}];
It's work fine. But it I change view ( I have scrollView and added a lot of view with animation) it show another view and it's own animation but [NSThread sleepForTimeInterval: 4.0f]; from previous page work on it's to.
So how I can make delay for my animation?
Thanks for help!

I personally like to rely on timers. They don't hang the app and give you more flexibility to control your triggers.
NSTimer *mytimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myAnimationFunction:) userInfo:nil repeats:NO];
-(void) myAnimationFunction:(NSTimer *)timer{<br>
// Do anything here...<br>
}

Related

Animation issue of Button in IOS

i have a three buttons in home view controller . When i click but1 it perform animation and move toward right with a duration to a specific width. When but1 reach to its given width it hides and shows but2 there. when i hit but2 it perform animation and move back to the same position of but1. Its like a scenario of open and close of button. The issue now i'm facing is that when i open but1 it moves its given width and when i close it move back to original but1 position , now buttons open and close but when we try to open again it does not open with but1. I want that whenever user click but1 it should moves towards right with animation and every time user want to close the button it should close it rather than open or close only one time. My code for opening of button is this,
- (IBAction)openHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
CGAffineTransform transform = CGAffineTransformMakeTranslation(108, 0);
[_openHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
}
-(void)targetMethod:(NSTimer *)myTimer{
_home.hidden=NO;
_openHome.hidden=YES;
_closeHome.hidden=NO;
[myTimer invalidate];
}
For closing the button its this,
- (IBAction)closeHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-110, 0);
[_closeHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:#selector(targetMethoded:)
userInfo:nil
repeats:NO];
}
-(void)targetMethoded:(NSTimer *)myTimer{
_home.hidden=YES;
_openHome.hidden=YES;
_closeHome.hidden=NO;
[myTimer invalidate];
}
The screen before open of button is this,
The screen after open of button,
Instead of using CGAffineTransform to change the position just change the buttons frame directly. Also use the following code which uses the completion handler to call the targetMethod instead of using the NSTimer:
[UIView animateWithDuration:0.75 animations:^{
_openHome.frame = CGRectMake(_openHome.frame.origin.x+110, _openHome.frame.origin.y, _openHome.frame.size.width, _openHome.frame.size.height);
} completion:^(BOOL finished) {
[self targetMethod];
}];

How to know if color "touch" another color

I'm trying to create something that if I have a UIView with background of yellow, and this view is moving on the screen, and somewhere placed another UIView with background of red. How can I know if the yellow color touch the red color? - which mean's that the first view touches the another view.
During the animation you will have to periodically check for intersection like #AMI289 has said. e.g.
- (void)animate {
// Use an NSTimer to check for intersection 10 times per second
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(checkForCollision) userInfo:nil repeats:YES];
[UIView animateWithDuration:5 animations:^{
// Do the animation
} completion:^(BOOL complete) {
// Tell timer to stop calling checkForCollision
[timer invalidate];
}];
}
- (void)checkForCollision {
if (CGRectIntersectsRect([viewOne.layer.presentationLayer frame], [viewTwo.layer.presentationLayer frame])) {
// Handle collision
}
}
It is important to get the presentation layer of the views that are being animated otherwise you will not detect incremental changes to the views' positions on the screen. You also need to invalidate the timer once you are done with it otherwise your checkForCollision method will continue to run indefinitely.

Showing every 5 seconds a new UIImageView with the help of NSTimer and UIView Animation?

I am trying to create a Calibration View! I have 12 calibration points as UIImageViews. Now I want to show every point for 5 seconds. So the whole calibration time is 1 minute. The Alpha of the ImageViews is set to 0.0! In the UI Animation I want to set the Alpha to 1.0 for one point after an other, for each point only for 5 seconds.
So far I did this (see the code)! But this animates me (fades in) after 5 seconds for 5 seconds all 12 Points at once!How can I solve this for one after another with a NSTimer and UI Animation? Thanks
-(id)init{
_calibrationViewArray = [[NSMutableArray alloc]init];
_calibrationPoint1 = [[UIImageView alloc]initWithFrame:(CGRectMake(115.5,113.0,25.0,25.0))];
_calibrationPoint1.backgroundColor = [UIColor redColor];
_calibrationPoint1.alpha = 0.0;
[self addSubview:_calibrationPoint1];
[_calibrationViewArray addObject:_calibrationPoint1];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
-(void)onTimer{
for (int i = 0; i < [_calibrationViewArray count]; i++) {
UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:i];
[UIView beginAnimations:#"Calibration" context:nil];
[UIView setAnimationDuration:5.0];
// Make the animatable changes.
currentCalibrationPoint.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
}
Declare a variable in your class to be:
int cont;
Initialize variable when is needed:
cont=0;
Change your code to this:
-(void)onTimer{
//Loop is not needed, with the timer and counter is enough
UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:cont];
[UIView beginAnimations:#"Calibration" context:nil];
[UIView setAnimationDuration:5.0];
// Make the animatable changes.
currentCalibrationPoint.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
if(cont>0){
//Hide previous view (here you can make another animation, instead of changing alpha right away)
[_calibrationViewArray objectAtIndex:cont-1].alpha = 0.0;
}
cont++;//+1
}
In your onTimer method you shouldn't be looping over all of the image views, you should have a currentIndex or similar variable so you can just get the next image view and run an animation on it alone.
Do not use timer at all.
Use the:
[UIView animateWithDuration: animations: completion:]
method.
In the animation block fade one of the dots, then in completion block run the animation again.

UIView's animateWithDuration delay not delaying animation

I am trying to perform an animation on a label where a flip animation happens and after it is done and after a delay, The text of the label changes.
It seems that the delay never happens. The text immediately changes after the flip completes although I am using UIView animateWithDuration:0.5 delay:4.0 in the completion block. If Instead I do a performSelector with Delay in the completion block (the commented statement) it works as expected. Any idea why the delay value is being ignored?
- (void) flipShapeWithText:(NSString *)text {
[UIView transitionWithView:someLabel duration:0.15 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
someLabel.text = text;
}completion:^ (BOOL finished){
// [self performSelector:#selector(updateLabelText:) withObject: #"New Text" afterDelay:4.0];
[UIView animateWithDuration:0.5
delay:4.0
options: UIViewAnimationOptionTransitionCrossDissolve
animations:^{
currentShapeNameLabel.text = #"New Text" ;}
completion:nil];
}];
}
The delay param of animateWithDuration:delay:options:animations:completion specifies the delay before the animation occurs. You are setting the text within the animation block so after the delay is over, the animations begin which immediately changes the text as that change is not animatable. To do what you want, change the text in the completion block as follows:
[UIView animateWithDuration:0.5
delay:4.0
options: UIViewAnimationOptionTransitionCrossDissolve
animations:^{ // anything animatable }
completion:^(BOOL finished) {
currentShapeNameLabel.text = #"New Text" ;}];
You can eliminate the delay if you want the animation to start immediately. If you want the text change to happen 4 secs after the animation completes add that delay in the completion block either with dispatch_after() or performSelector:withDelay:.
In my case, the problem was that earlier in the code I was calling UIView's snapshotViewAfterScreenUpdates with a value true. After changing that to false it worked fine.
try nesting in
dispatch_async(dispatch_get_main_queue(), ^{
});

IOS: set alpha in a UIImage

I have an UIImage and I want to set its alpha at 0.00 after 3 seconds... In my UIImage there is a picture that I want to immediately see when I go in a view, but after 3 second it must disappear.
Easy as that using core animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3.0f];
imageView.alpha = 0.0f;
[UIView commitAnimations];
if you dont want to slowly fade within 3 seconds, you can use
[UIView setAnimationDelay:3];
and reduce the animation duraction to 0.5f or something.
i think using a short fade out time feels better than just simply set hide to YES
This will make it fade out within three seconds. Call from viewDidLoad:
[UIView animateWithDuration:3.0 animations:^{myImage.alpha = 0; }];
Or if you want the animation to start at 2.5 seconds and last half a second, you could put that into a method (changing 3.0 to 0.5) and then call:
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(hideMyImage) userInfo:nil repeats:no];
Are you showing your UIImage in a UIImageView?
If so, just set the hidden property on that image view to YES and the image view (with its image) will disappear.
You can always remove the sub-child view from the parent.
[imageView removeFromSuperview];
you can always add it later with
[self.view addSubview:imageView];

Resources