Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-31-2009, 02:18 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 32
MrFocus is on a distinguished road
Default Animation Loop question

Hi all,

I am trying to create an application that will change the speed of a pulsating animation based on input from a slider control.
The first animation loop works great, but then any change to the slider does not impact the animation at all.
Any ideas? (code below)

- (IBAction)sliderChange {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:-0.02*mySlider.value+2];

CGAffineTransform transform =
CGAffineTransformMakeScale(1.5,1.5);
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatCount:1e100f];
pulsatingImageView.transform=transform;
[UIView commitAnimations];


}
MrFocus is offline   Reply With Quote
Old 03-31-2009, 03:12 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by MrFocus View Post
Hi all,

I am trying to create an application that will change the speed of a pulsating animation based on input from a slider control.
The first animation loop works great, but then any change to the slider does not impact the animation at all.
Any ideas? (code below)

- (IBAction)sliderChange {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:-0.02*mySlider.value+2];

CGAffineTransform transform =
CGAffineTransformMakeScale(1.5,1.5);
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatCount:1e100f];
pulsatingImageView.transform=transform;
[UIView commitAnimations];


}
What do you have for the minimum and maximum values for the slider? By default, those are 0.0 and 1.0, which means the range of your animation duration is 2.0 to 2.02. That's only a 1% change. Probably not noticeable.

Did you really mean to do [UIView setAnimationDuration:-0.02*(mySlider.value+2)];, which would give you a range of 0.04 to 0.06?

joe
FlyingDiver is offline   Reply With Quote
Old 03-31-2009, 03:16 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 32
MrFocus is on a distinguished road
Default

Hi Joe,

My slider goes from 0 to 100, so the effect would be quite noticeable. The reason for the math is that I want to pulse between 2 seconds and .2 second intervals.

Thanks,

B

Quote:
Originally Posted by FlyingDiver View Post
What do you have for the minimum and maximum values for the slider? By default, those are 0.0 and 1.0, which means the range of your animation duration is 2.0 to 2.02. That's only a 1% change. Probably not noticeable.

Did you really mean to do [UIView setAnimationDuration:-0.02*(mySlider.value+2)];, which would give you a range of 0.04 to 0.06?

joe
MrFocus is offline   Reply With Quote
Old 03-31-2009, 08:50 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 32
MrFocus is on a distinguished road
Default

Well, after playing with this for a good while (trying grow/shrink animations separately, using [UIView setAnimationDidStopSelector etc.), I have given up and am using a UIImageView.animation instead. While it isn't as pretty as my original .PNG file that used the CGAffineTransform, I have more control over changing the speed of the animation in mid-loop.

If anybody can point me towards a solution of my original problem, please let me know.


Quote:
Originally Posted by MrFocus View Post
Hi Joe,

My slider goes from 0 to 100, so the effect would be quite noticeable. The reason for the math is that I want to pulse between 2 seconds and .2 second intervals.

Thanks,

B
MrFocus is offline   Reply With Quote
Old 05-15-2009, 06:05 PM   #5 (permalink)
Registered Member
 
Join Date: May 2009
Location: Maryland
Posts: 5
Rolf Hendriks is on a distinguished road
Default Solution to a SIMILAR problem

i was playing around with this for a while, too. i wanted to play a small and subtle pulse animation on an object most of the time, but occasionally have a strong and accentuated pulse animation. so i wanted a general purpose pulsating function. the solution i came up with was this:

first, i define all the parameters i want to adjust:

Code:
struct pulseAnimation{
	float durationInSeconds;
	float expansionFactor;
	int numRepetitions;
	UIViewAnimationCurve expandCurve;
	UIViewAnimationCurve contractCurve;
};
there are some slight differences in how i animate the pulse effect. first of all, i use a numRepetitions variable instead of looping my pulse animations indefinitely. To get something to pulse every second, i'd manually call my pulse function with 1 repetition every second by using an update loop or a timer.

Then, to animate an object, i use:

Code:
// expansion part of a pulse animation
- (void)pulse:(pulseAnimation*)animationData{
	// memory management issue: we need to retain a COPY of the //animation info because 
	//	we overwrite some of its data while animating, and because
	//	the animationInfo could be the address of a local variable! to //encapsulate this, 
	//	we just store a copy of the data as a member variable.
	if (animationData)
		pulse = *animationData;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:pulse.durationInSeconds];
	[UIView setAnimationCurve:pulse.expandCurve];
	// scale the object to its expanded size
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(pulse_reverse)];
	[startButton setTransform:CGAffineTransformMakeScale(pulse.expansionFactor, pulse.expansionFactor)];
	[UIView commitAnimations];
}

// contraction part of a pulse animation. This gets called automatically after the 
//	expansion part finishes - not intended to be called manually.
- (void)pulse_reverse{	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:pulse.durationInSeconds];
	[UIView setAnimationCurve:pulse.contractCurve];
	// object is at maximum expanded size, so revert back to original size
	[startButton setTransform:CGAffineTransformIdentity];
	// after doing this contraction, a pulse animation is completed.
	//	but, we have a feature that allows us to play more than one pulse animation in a row...
	--pulse.numRepetitions;
	if (pulse.numRepetitions > 0){
		[UIView setAnimationDelegate:self];
		[UIView setAnimationDidStopSelector:@selector(pulse:)];
	}
	[UIView commitAnimations];
	
}

To use this function, I'd do something like:
Code:
pulseAnimation bigPulse = {
   // fill in the data here...
};
[self pulse:&bigPulse];

Note that the above code is in a viewController and the object i'm animating (startButton) is hardcoded. This is definitely not ideal, but since i'm only animating one object, it's good enough for me.

i originally had everything in one big animation function that used the animation name and context, but as always i found that things become much easier if you split your task into specific functions that take as few parameters as possible.
Rolf Hendriks is offline   Reply With Quote
Old 08-08-2009, 12:23 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 32
MrFocus is on a distinguished road
Default

I implemented your suggestion (hey, only a few months later ...works great, except:
On the simulator, there seems to be a slow allocation leak, with GeneralBlocks being incrementally allocated by Quartz and the memory footprint creeping up by about 200K / minute.
Not a big deal if the app runs for a few minutes, but might be a serious problem after about 20 minutes.
These are NOT memory leaks but rather ongoing allocation requested by Quartz...any thoughts?
MrFocus is offline   Reply With Quote
Old 08-08-2009, 01:11 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 32
MrFocus is on a distinguished road
Default SOLVED...it's an instrument bug, not an actual allocation or memory leak

Phew!
Issue 391 - cocos2d-iphone - Object Alloc keeps raising in 3.0beta5 OS - Project Hosting on Google Code
MrFocus is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, duration, iphone, loop

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
5 members and 324 guests
blueorb, guusleijsten, Kryckter, LEARN2MAKE, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0