Chain animations in UIVIEW inside of for loop - animation

I have some problems with animations. Here is what I am trying to do.
First I have a for loop where I make some animations of UILabels, for example
label1.frame = CGRectMake(xBoard, 5, textWidth, term1Label.frame.size.height);
xBoard = xBoard + textWidth;
label1.textColor = term1Label.textColor;
label1.font = [UIFont italicSystemFontOfSize:textHeight];
label1.textAlignment = term1Label.textAlignment;
label1.text = label1String;
label1.alpha = 0;
label1.backgroundColor = [UIColor clearColor];
[boardView addSubview:label1];
[UIView animateWithDuration:2.0*time
delay:timeDelay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label1.alpha = 1;
} completion:^(BOOL finished){
}
];
timeDelay = timeDelay + 2.0*time;
Some labels appear, some disappear, some change color. Everything is working fine until I make second loop. Then the first part of code just appear (the animations of changing color are still working but all labels from first loop are visible and do not animate. Second loop animates fine.
I was trying to replace the animations [UIView animateWithDuration:...] with CABasicAnimations and save them in one NSMutableArray with queue, but I am not successful. Probably my knowledge is too short yet. Any help is welcome. Thanks!!!

I am in the middle of solving my problem. Doing my loops I am saving animation parameters in the table
NSArray *animation1 = [[NSArray alloc]initWithObjects:[NSNumber numberWithInt:label1.tag], [NSNumber numberWithInt:3],[NSNumber numberWithFloat:0.5f*time],[NSNumber numberWithFloat:timeDelay],[NSNumber numberWithFloat:0],nil];
[animations addObject:animation1];
in the end of void I call [self performAnimations];
- (void)performAnimations {
if ([[self animations] count] == 0) return;
for (int i=0; i<[[self animations] count]; i++) {
int tag = [[[animations objectAtIndex:i]objectAtIndex:0]intValue];
int animation = [[[animations objectAtIndex:i]objectAtIndex:1]intValue];
float animTime = [[[animations objectAtIndex:i]objectAtIndex:2]floatValue];
float animDelay = [[[animations objectAtIndex:i]objectAtIndex:3]floatValue];
float parameter = [[[animations objectAtIndex:i]objectAtIndex:4]floatValue];
UILabel *label = [UILabel alloc];
UIView *view = [UIView alloc];
if ([[self.view viewWithTag:tag] isKindOfClass:[UILabel class]]){
label = (UILabel *)[self.view viewWithTag:tag];
} else if ([[self.view viewWithTag:tag] isKindOfClass:[UIView class]]) {
view = (UIView *)[self.view viewWithTag:tag];
}
switch (animation) {
case 1:
[PolyCalcsViewController colorizeLabelForAWhile:label withUIColor:[UIColor yellowColor] animated:YES withTime:animTime withDelay:animDelay];
break;
case 2:
[PolyCalcsViewController appear:label withTime:animTime withDelay:animDelay];
break;
case 3:
[PolyCalcsViewController disappear:label withTime:animTime withDelay:animDelay];
break;
case 4:
[PolyCalcsViewController moveView:view withOffset:parameter withTime:animTime withDelay:animDelay];
break;
default:
break;
}
}
[animations removeAllObjects];
}
+(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated withTime:(float)time withDelay:(float)delay{
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
tempLabel.backgroundColor = [UIColor clearColor];
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;
if (animated) {
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate it
label.alpha = 0;
tempLabel.alpha = 1;
} completion:^(BOOL finished){
[UIView animateWithDuration:time
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate it back.
label.alpha = 1;
tempLabel.alpha = 0;
} completion:^(BOOL finished){
// Remove the tempLabel view when we are done.
[tempLabel removeFromSuperview];
}];
}];
} else {
// Change it back at once and remove the tempLabel view.
label.alpha = 1.0;
[tempLabel removeFromSuperview];
}
}
+(void)appear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label.alpha = 1.0;
} completion:^(BOOL finished){
NSLog(#"Anim Appear %d",label.tag);
}];
}
+(void)disappear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished){
NSLog(#"Anim Disappear %d",label.tag);
[label removeFromSuperview];
}];
}
The point is that program is not performing animations in sequence how it is in the table animations. I think I should give up of time delay and run the next animation when the previous one is finished. I could make a counter in the completion:(BOOL finished) block, but I do not know how because the function doesn't accept external variable. Any ideas how to detect the end of animation and control the table execution? Thanks.

Finally I have solved the problem. Here is a correctly working code
- (void)performAnimations {
// Finish when there are no more animations to run
int i = animationNumber;
int tag = [[[animations objectAtIndex:i]objectAtIndex:0]intValue];
int animation = [[[animations objectAtIndex:i]objectAtIndex:1]intValue];
float animTime = [[[animations objectAtIndex:i]objectAtIndex:2]floatValue];
//float animDelay = [[[animations objectAtIndex:i]objectAtIndex:3]floatValue];
float animDelay = 0;
float parameter = [[[animations objectAtIndex:i]objectAtIndex:4]floatValue];
UILabel *label = [UILabel alloc];
UIView *view = [UIView alloc];
if ([[self.view viewWithTag:tag] isKindOfClass:[UILabel class]]){
label = (UILabel *)[self.view viewWithTag:tag];
} else if ([[self.view viewWithTag:tag] isKindOfClass:[UIView class]]) {
view = (UIView *)[self.view viewWithTag:tag];
}
switch (animation) {
case 1:
[self colorizeLabelForAWhile:label withUIColor:[UIColor yellowColor] animated:YES withTime:animTime withDelay:animDelay];
break;
case 2:
[self appear:label withTime:animTime withDelay:animDelay];
break;
case 3:
[self disappear:label withTime:animTime withDelay:animDelay];
break;
case 4:
[self moveView:view withOffset:parameter withTime:animTime withDelay:animDelay];
break;
default:
break;
}
}
-(void)animationDidFinished{
animationNumber = animationNumber + 1;
if (animationNumber == [[self animations] count]) {
[animations removeAllObjects];
return;
} else {
[self performAnimations];
}
}
-(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated withTime:(float)time withDelay:(float)delay{
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
tempLabel.backgroundColor = [UIColor clearColor];
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;
if (animated) {
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate it
label.alpha = 0;
tempLabel.alpha = 1;
} completion:^(BOOL finished){
[UIView animateWithDuration:time
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate it back.
label.alpha = 1;
tempLabel.alpha = 0;
} completion:^(BOOL finished){
// Remove the tempLabel view when we are done.
[tempLabel removeFromSuperview];
if (finished) {
[self animationDidFinished];
}
}];
}];
} else {
// Change it back at once and remove the tempLabel view.
label.alpha = 1.0;
[tempLabel removeFromSuperview];
}
}
-(void)appear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label.alpha = 1.0;
} completion:^(BOOL finished){
NSLog(#"Anim Appear %d",label.tag);
if (finished) {
[self animationDidFinished];
}
}];
}
-(void)disappear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished){
NSLog(#"Anim Disappear %d",label.tag);
[label removeFromSuperview];
if (finished) {
[self animationDidFinished];
}
}];
}
-(void)moveView:(UIView *)view withOffset:(float)offset withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame = view.frame;
frame.origin.y += offset;
view.frame = frame;
} completion:^(BOOL finished){
if (finished) {
[self animationDidFinished];
}
}
];
}

Related

GPUImageStillCamera image preview jumps when taking a photo

I am taking a square cropped photo with GPUImageStillCamera and allowing the user to zoom the camera. When the user clicks to take a picture the camera jumps forward for a split second (as if the camera zoomed in even further past the area the user zoomed to and then immediately returns to the correct crop once the image is returned to screen). This only happens when the user has zoomed the camera. If they have not zoomed the camera the flicker/jump does not happen. (The image return has the correct crop whether or not the user has zoomed).
Thoughts?
Creating camera and adding square crop
//Add in filters
stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
//Creating a square crop filter
cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0.f, (720.0f/1280.0f)/2.0f, 1.f, (720.0f/1280.0f))];
Image zoom method
-(void)imagePinch:(UIPinchGestureRecognizer *)recognizer{ //Controlling the zoom scale as the user pinches the live preview
if (recognizer.state == UIGestureRecognizerStateBegan) {
zoomOutAdder = 0.0f;
if (currentScale > 2) {
zoomOutAdder = currentScale;
}
}
float addition = (recognizer.scale - lastScale);
if (addition > 0) {
addition = addition *1.7;
}
if (addition < 0) {
addition = addition *(1.7+zoomOutAdder);
}
currentScale = currentScale +addition;
lastScale = recognizer.scale;
if (currentScale < 1) {
currentScale = 1;
}
if (currentScale > 4) {
currentScale =4;
}
if (currentScale == 1) {
zoomOutAdder = 0.0f;
}
cameraImagePreview.transform = CGAffineTransformMakeScale(currentScale, currentScale);
if (recognizer.state == UIGestureRecognizerStateEnded) {
lastScale = 1.0f;
}
Take a photo method
//Adjust crop based on zoom scale of the user
CGFloat zoomReciprocal = 1.0f / currentScale;
CGPoint offset = CGPointMake(((1.0f - zoomReciprocal) / 2.0f), (((1.0f- zoomReciprocal)*(720.0f/1280.0f)) / 2.0f) + ((720.0f/1280.0f)/2)) ;
CGRect newCrop = cropFilter.cropRegion;
newCrop.origin.x = offset.x;
newCrop.origin.y = offset.y;
newCrop.size.width = cropFilter.cropRegion.size.width * zoomReciprocal;
newCrop.size.height = cropFilter.cropRegion.size.height *zoomReciprocal;
cropFilter.cropRegion = newCrop;
*/
//Place photo inside an image preview view for the user to decide if they want to keep it.
[stillCamera capturePhotoAsImageProcessedUpToFilter:cropFilter withOrientation:imageOrientation withCompletionHandler:^(UIImage *processedImage, NSError *error) {
//Pause the current camera
[stillCamera pauseCameraCapture];
//Rest of method
ADDED METHODS
- (void) flipCamera {
if (stillCamera.cameraPosition != AVCaptureDevicePositionFront) {
[UIView animateWithDuration:.65 animations:^{
flipCamera.transform = CGAffineTransformMakeScale(-1, 1);
}];
} else {
[UIView animateWithDuration:.65 animations:^{
flipCamera.transform = CGAffineTransformMakeScale(1, 1);
}];
}
[self performSelector:#selector(rotateCamera) withObject:0 afterDelay:.2];
}
- (void) rotateCamera {
[stillCamera rotateCamera];
//Adjust flash settings as needed
[stillCamera.inputCamera lockForConfiguration:nil];
if (stillCamera.cameraPosition != AVCaptureDevicePositionFront) {
[stillCamera.inputCamera setFlashMode:AVCaptureFlashModeOff];
}
NSAttributedString *attributedFlash =
[[NSAttributedString alloc]
initWithString:#"off"
attributes:
#{
NSFontAttributeName : [UIFont fontWithName:#"Roboto-Regular" size:13.0f],
NSForegroundColorAttributeName : [UIColor colorWithWhite:1 alpha:.55],
NSKernAttributeName : #(.25f)
}];
flashLabel.attributedText = attributedFlash;
[UIView animateWithDuration:.2 animations:^{
[flash setTintColor:[UIColor colorWithWhite:1 alpha:.55]];
}];
[stillCamera.inputCamera unlockForConfiguration];
}
- (void) changeFlash {
if (stillCamera.cameraPosition == AVCaptureDevicePositionFront) {//no flash available on front of camera
return;
}
[stillCamera.inputCamera lockForConfiguration:nil];
if (stillCamera.inputCamera.flashMode == AVCaptureFlashModeOff) {
[stillCamera.inputCamera setFlashMode:AVCaptureFlashModeOn];
[self animateFlashWithTintColor:[UIColor colorWithWhite:1 alpha:1] andString:#"on"];
} else if (stillCamera.inputCamera.flashMode == AVCaptureFlashModeOn) {
[stillCamera.inputCamera setFlashMode:AVCaptureFlashModeOff];
[self animateFlashWithTintColor:[UIColor colorWithWhite:1 alpha:.55] andString:#"off"];
}
[stillCamera.inputCamera unlockForConfiguration];
}
- (void) animateFlashWithTintColor:(UIColor *)color andString:(NSString *)text {
//Set new text
NSAttributedString *attributedFlash =
[[NSAttributedString alloc]
initWithString:text
attributes:
#{
NSFontAttributeName : [UIFont fontWithName:#"Roboto-Regular" size:13.0f],
NSForegroundColorAttributeName : [UIColor colorWithWhite:1 alpha:.55],
NSKernAttributeName : #(.25f)
}];
flashLabel.attributedText = attributedFlash;
float duration = .7;
[UIView animateKeyframesWithDuration:duration delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:duration animations:^{
[flash setTintColor:color];
}];
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:.7/duration animations:^{
flash.transform = CGAffineTransformMakeRotation(M_PI);
}];
}completion:^(BOOL finished){
flash.transform = CGAffineTransformIdentity;
}];
}
-(void) usePhoto {
if ([ALAssetsLibrary authorizationStatus] != ALAuthorizationStatusAuthorized){
NSLog(#"Do Not Have Right To Save to Photo Library");
}
//Save Image to Phone Album & save image
UIImageWriteToSavedPhotosAlbum(takenPhoto.image, nil, nil, nil);
//Save Image to Delegate
[self.delegate saveImageToDatabase:takenPhoto.image];
[self performSelector:#selector(dismissCamera) withObject:0 afterDelay:.4];
}
Some additional code showing the creation of the the various camera elements used to capture a photo.
centerPoint = CGPointMake(self.view.frame.size.width/2, (cameraHolder.frame.size.height+50+self.view.frame.size.height)/2);
cameraImagePreview = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, cameraHolder.frame.size.width, cameraHolder.frame.size.width)];
[cameraHolder addSubview:cameraImagePreview];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTouch:)];
[cameraImagePreview addGestureRecognizer:tapGesture];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(imagePinch:)];
[cameraImagePreview addGestureRecognizer:pinchGesture];
float scaleForView = self.view.frame.size.width/720.0;
fullCameraFocusPoint = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1280*scaleForView)];
fullCameraFocusPoint.center = CGPointMake(cameraHolder.frame.size.width/2, (cameraHolder.frame.size.width/2)+50);
[self.view insertSubview:fullCameraFocusPoint atIndex:0];
takenPhoto = [[UIImageView alloc]initWithFrame:cameraHolder.frame];
takenPhoto.alpha = 0;
[self.view addSubview:takenPhoto];
//Add in filters
stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
//Creating a square crop filter
cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0.f, (720.0f/1280.0f)/2.0f, 1.f, (720.0f/1280.0f))];
//Create standard vignette filter
vignetteFilter = [[GPUImageVignetteFilter alloc] init]; //1
vignetteFilter.vignetteCenter = CGPointMake(.5, .5);
vignetteFilter.vignetteStart = 0.4f;
vignetteFilter.vignetteEnd = 1.08f;
//Add filters to photo
[cropFilter addTarget:vignetteFilter];
[stillCamera addTarget:cropFilter];
[vignetteFilter addTarget:cameraImagePreview];
[stillCamera startCameraCapture];

Animation Delay

I am trying to set a delay for my animation, so once it appears and then disappears, I want to wait a certain amount of seconds for it to reappear. I have tried placing it in multiple spots throughout my code, but it was all the same result.
- (void) startRedDot {
redDotTimer = [NSTimer scheduledTimerWithTimeInterval:1.5
target:self
selector:#selector(moveButtonWithAnimation)
userInfo:nil
repeats:YES];
[redDotTimer fire];
}
-(void) moveButtonRandomly {
CGSize limits;
CGPoint newPosition;
// Get limits
limits.width = gameView.frame.size.width - redButton.frame.size.width;
limits.height = gameView.frame.size.height - redButton.frame.size.height;
// Calculate new Position
newPosition = (CGPoint){ limits.width * drand48(), limits.height * drand48() };
// Set new frame
redButton.frame = (CGRect){ newPosition, redButton.frame.size };
}
- (void) moveButtonWithAnimation {
CGFloat fadeDurration;
if ( !redButton )
return; // only invoke the button if it exists
fadeDurration = 0.0f;
//Fade Out
[UIView animateWithDuration: fadeDurration animations:^{
redButton.alpha = 0.0f;
} completion:^(BOOL finished) {
// Move the button while it is not visible
[self moveButtonRandomly];
[UIView setAnimationDelay:9.0];
// Fade in
[UIView animateWithDuration: fadeDurration animations:^{
redButton.alpha = 4.0f;
}];
}];
}
setAnimationDelay should use in beginAnimation and commitAnimation block, and it was old way to do animation in iOS. In your case try this:
- (void) moveButtonWithAnimation {
CGFloat fadeDurration;
if ( !redButton )
return; // only invoke the button if it exists
fadeDurration = 2.0f;
//Fade Out
[UIView animateWithDuration: fadeDurration animations:^{
redButton.alpha = 0.0f;
} completion:^(BOOL finished) {
// Move the button while it is not visible
[self moveButtonRandomly];
[UIView animateWithDuration:fadeDurration delay:2.0 options:0 animations:^{
redButton.alpha = 1.0f;
} completion:nil];
}];
}

Reset nested uiscrollview zooms when scroll ends

Nested uiscrolls in a larger uiscroll need to, when zoomed, reset zoom level when they are off screen. I am trying to reset all of them when the scrolling ends but no luck. Any ideas?
myScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
myScrollview.pagingEnabled = YES;
myScrollview.scrollEnabled =YES;
myScrollview.clipsToBounds = NO;
myScrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollview.showsHorizontalScrollIndicator = YES;
myScrollview.backgroundColor = [UIColor blackColor];
myScrollview.delegate = self;
NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"01.png"],[UIImage imageNamed:#"02.png"],[UIImage imageNamed:#"03.png"],[UIImage imageNamed:#"04.png"],nil];
for (int i = 0; i <viewcount; i++)
{
CGFloat x = i * self.view.frame.size.width;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
[subView setBackgroundColor:[UIColor blackColor]];
[subView setCanCancelContentTouches:NO];
subView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
[self.aImageView setTag:viewcount];
[subView addSubview:aImageView];
[subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
subView.minimumZoomScale = 1;
subView.maximumZoomScale = 3;
subView.delegate = self;
[subView setScrollEnabled:YES];
subView.contentSize = aImageView.frame.size;
[myScrollview addSubview:subView];
}
myScrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);
[self.view addSubview:myScrollview];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog (#"test");
UIView * view = nil;
view = [subView viewWithTag:0];
//return view;
return [scrollView.subviews objectAtIndex:0];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"Did scroll");
[self resetImageZoom];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
= NSLog(#"Did end decal");
[self resetImageZoom];
}
-(void)resetImageZoom {
NSLog(#"Resetting any image zoom");
for(UIView *view in [myScrollview subviews]) {
//if([view isKindOfClass:[UIScrollView class]]) {
//[(UIScrollView*)view setZoomScale:1.0 animated:NO];
//}
view.transform = CGAffineTransformIdentity;
}
}
That did it...
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(#"Did end dece");
for (UIView *view in scrollView.subviews) {
if([view isKindOfClass:[UIScrollView class]]) {
[(UIScrollView*)view setZoomScale:1.0 animated:NO];
}
}
}

How to create a flashing record button for iPhone

I've looked at several posts, including [this one].1 This is exactly what I want to do, but I cannot get it to work.
I want to show the user that they have pressed a record button, and that they will need to press it again to stop recording.
So far, the code for the start recording method looks like this:
NSLog(#"DetailVC - recordAudio - soundFilePath is %#", soundFile);
[audioRecorder prepareToRecord];
recording = YES;
NSLog(#"start recording");
NSLog(#"1");
//[autoCog startAnimating];
[audioRecorder recordForDuration:120];
recording = NO;
//Start a timer to animate the record images
if (recording == YES) {
toggle = FALSE;
timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
target: self
selector: #selector(toggleButtonImage:)
userInfo: nil
repeats: YES];
//UIImage *changeImage = [UIImage imageNamed:stopButtonFile];
//[recordButton setImage:changeImage forState:UIControlStateNormal];
//[timer invalidate];
//timer = nil;
NSLog(#"2");
}
and the toggleButton method looks like this:
- (void)toggleButtonImage:(NSTimer*)timer
{
NSLog(#"%s", __FUNCTION__);
if(toggle)
{
NSLog(#"1");
/*
[UIView beginAnimations];
recordButton.opacity = 0.0;
[recordButton setAnimationDuration: 1.5];
[recordButton commitAnimations];
[recordButton setImage:[UIImage imageNamed:#"record.png"] forState: UIControlStateNormal];
*/
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //delete "EaseOut", then push ESC to check out other animation styles
[UIView setAnimationDuration: 0.5];//how long the animation will take
[UIView setAnimationDelegate: self];
recordButton.alpha = 1.0; //1.0 to make it visible or 0.0 to make it invisible
[UIView commitAnimations];
}
else
{
NSLog(#"2");
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //delete "EaseOut", then push ESC to check out other animation styles
[UIView setAnimationDuration: 0.5];//how long the animation will take
[UIView setAnimationDelegate: self];
recordButton.alpha = 0.0; //1.0 to make it visible or 0.0 to make it invisible
[UIView commitAnimations];
//[recordButton setImage:[UIImage imageNamed:#"stopGrey.png"] forState: UIControlStateNormal];
}
toggle = !toggle;
}
I get to see the logs showing that the loop is iterating, but:
the images do not switch, and
the operation is not running in parallel with the record method.
I would appreciate any ideas as to how to proceed.
Expanding on Ashley's answer, I'd go with something more like this:
- (IBAction)record:(id)sender {
self.recording = !self.recording;
if (!self.recording) {
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.recordButton.alpha = 1.0f;
}
completion:^(BOOL finished){
}];
} else {
self.recordButton.alpha = 1.0f;
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.recordButton.alpha = 0.0f;
}
completion:^(BOOL finished){
}];
}
}
Try this:
- (IBAction)record:(id)sender
{
self.recording = !self.recording;
[self toggleButton];
}
- (void) toggleButton
{
if (!self.recording) {
self.recordButton.alpha = 1.0;
return;
}
[UIView animateWithDuration: 0.5
delay: 0.0
options: UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.recordButton.alpha = !self.recordButton.alpha;
}
completion:^(BOOL finished) {
[self toggleButton];
}];
}

How To Move UIImageView?

Heres the deal:
I code an UIImageView in the viewDidLoad and i want it to move down with my fonction buttonPressed** without creating an other subview like i do.
Heres the code.
-(void)viewDidLoad {
[super viewDidLoad];
banetteImage = [UIImage imageNamed:#"myBanette.png"];
UIImageView *banetteView = [[UIImageView alloc] initWithImage:banetteImage];
banetteView.frame = CGRectMake(100, -740, 568, 790);
banetteView.opaque = NO;
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(buttonPressed: ) userInfo:nil repeats:NO];
[self.view addSubview:banetteView];
}
-(void)buttonPressed {
double speed = 1 / round(random() % 100) + 1.0;
[UIView beginAnimations:nil context:banetteView];
[UIView setAnimationDuration: 2*speed ];
banetteView.frame = CGRectMake(100, -2, 568, 790);
banetteView.opaque = NO;
UIImageView *banetteView = [[UIImageView alloc] initWithImage:banetteImage];
banetteView2.frame = CGRectMake(100, -740, 568, 790);
banetteView.opaque = NO;
banetteView.hidden = YES;
[self.view addSubview:banetteView];
// set a stop callback so we can cleanup the banette when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
-(void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIImageView *banetteView = context;
double speed = 1 / round(random() % 100) + 1.0;
banetteView.frame = CGRectMake(100, -2, 568, 790);
banetteView2.opaque = NO;
[self.view addSubview:banetteView2];
[UIView beginAnimations:nil context:banetteView];
[UIView setAnimationDuration: 2*speed ];
[banetteView release];
}
I found by adapting this code
http://mobile.tutsplus.com/tutorials/iphone/iphone-sdk-learning-about-touch-events-basic-game-animation/

Resources