Shrink UIButton - iphone

I have a application that runs a timer and performs a action 30 times a second. What I want to do is change the size of a UIButton i have so that every time the timer goes around, it changes the UIButton so that it is a little bit smaller. I have played with a bunch of things I have found online and I still cant figure it out.
Any ideas?

So, to move the comment out - is this generally what you're trying to do?
-(void) calledWhenTimerGoesRound
{
NSLog(#"calledWhenTimerGoesRound");
[UIView beginAnimations:nil context:#"MyAnimation"];
CGRect tempFrame = myButton.frame;
tempFrame.size.width = tempFrame.size.width - 5.0f;
tempFrame.size.height = tempFrame.size.height - 5.0f;
myButton.frame = tempFrame;
[UIView commitAnimations];
}
What does your timer code look like? Here's and example of what should work (resize the button smaller every second):
- (void) startMyTimer
{
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(calledWhenTimerGoesRound) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

Try something like:
CGRect tempFrame = myButton.frame;
myButton.frame = CGRectInset(tempFrame,5,5);

Related

get coordinates of animated UIImageview

I am animating an UIImageview in horizontal position for this purpose i have used the below code i have used the NSTimer
timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
-(void)onTimer
{
[UIView animateWithDuration:10.0f animations:^{
//Moving_Cloud is an image view
Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
}];
}
now the problem i am facing is i need to get the coordinates of the "Moving_Cloud" after the animate duration
please help me
Thanks in advance.
Abhijit Chaudhari from the comment in my previous post I understand that what you want is not the position "after the animate duration" but instead the position during the animation.
If I still didn't get it right please clarify your question. (Or buy me an new brain)
-(void) animateMe(){
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
[UIView animateWithDuration:10.0f animations:^{
//Moving_Cloud is an image view
Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
}];
}
-(void)onTimer:(id)sender{
NSLog(#"Currently in x=%f", [[ddddd.layer presentationLayer] frame].origin.x);
NSLog(#"Currently in y=%f", [[ddddd.layer presentationLayer] frame].origin.y);
}
[UIView animateWithDuration:10.0f animations:^{
Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
} completion:^(BOOL finished){
CGPoint newPost = Moving_Cloud.frame.origin;
CGFloat xPos = newPost.x;
CGFloat yPos = newPost.y;
// do stuff ?
}];
At the end of your animation the block "completion:" will be trigger.
Normally your image should be in x = 200, y = 150.
Be aware that those coordinates are relative to it superview (the view wrapping Moving_Cloud view).
Note:
By convention I recommend changing "Moving_Cloud" to "movingCloud".
Instance class start in lower cap in objective-C.
Also don't use _ but a capital letter instead.

How to autoscroll image by image?

I have about 9 images on the scrollView, i want to make it autoscrolling image by image(like first image scroll to second and ..till the last one). I did something like this:
timer = [NSTimer scheduledTimerWithTimeInterval:.0 target:self selector:#selector(scrolling) userInfo:nil repeats:NO];
- (void)scrolling{
CGFloat currentOffset = scrollView.contentOffset.x;
if(currentOffset < 2236){
CGFloat newOffset = currentOffset + 172;
[UIScrollView beginAnimations:nil context:NULL];
[UIScrollView setAnimationDuration:2.1];
[scrollView setContentOffset:CGPointMake(newOffset,0.0) animated:YES];
[UIScrollView commitAnimations];
}
but it just scroll only one time (from first image to the second). what did i do wrong? Any idea?
Set repeats to YES. It's the last parameter. Also, make sure the interval is not 0. (the posted code shows .0)
timer = [NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(scrolling)
userInfo:nil
repeats:YES];

Stop animation on UIScrollview

I've set up some code to scroll (paging) the scrollview automatically, after a TimeInterval. Now I want to stop the scrollview animating, after 4 times. Could someone teach me how to do that?
This is my code.
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
- (void) onTimer {
// Updates the variable h, adding 320
abc += 320;
//This makes the scrollView scroll to the desired position
[UIView animateWithDuration:1.5f animations:^{
[scrollView setContentOffset:CGPointMake(abc, 0) animated:NO];
}];
}
First add an ivar to an NSTimer
{
NSTimer *timer;
}
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
Then in the onTimer
- (void) onTimer {
//Create a static int
static int repetition = 0;
repetition ++;
if(repetition == 4)
{
repetition = 0;
//Stop the timer
[timer invalidate];
}
// Updates the variable h, adding 320
abc += 320;
//This makes the scrollView scroll to the desired position
[UIView animateWithDuration:1.5f animations:^{
[scrollView setContentOffset:CGPointMake(abc, 0)animated:NO];
}];
}

iPhone animation delay problem

I'm trying to get this animation to delay for 60 seconds and take 125 seconds to complete it's animation cycle. then repeat infinitely. the problem is that the delay only lasts 20 seconds. Is there a limit on the delay you can specify? or, perhaps a better way to do what I'm attempting?
here's my code:
- (void)firstAnimation {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"f1.png"],
[UIImage imageNamed:#"f2.png"],
[UIImage imageNamed:#"f3.png"],
[UIImage imageNamed:#"f4.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 400)];
myAnimatedView.animationImages = myImages;
[UIView setAnimationDelay:60.0];
myAnimatedView.animationDuration = 125.0;
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[self.view sendSubviewToBack:myAnimatedView];
[myAnimatedView release];
}
thanks for any help.
You're using the setAnimationDelay method in the wrong way.
setAnimationDelay is intended to be used when animating changes to animatable properties on views within a UIViewAnimations block like so:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:60];
//change an animatable property, such as a frame or alpha property
[UIView commitAnimations];
That code will delay the animation of a property change by 60 seconds.
If you want to delay a UIImageView from animating its images you'll need to use an NSTimer.
[NSTimer scheduledTimerWithTimeInterval:60
target:self selector:#selector(startAnimations:)
userInfo:nil
repeats:NO];
Then define the startAnimations: selector, like so:
- (void)startAnimations:(NSTimer *)timer
{
[myAnimatedView startAnimating];
}
That way, after 60 seconds, the timer will fire the method startAnimations: which will start your image view animating.

Image Intro at iphone application starting

Hi guys I want to create an intro image (fade in and fade out ) for my application.
Here is my code but, when I run the app on the device I have a problem:
My main nib page has been displayed
My image fades in and then fades out and then again fades in and fades out 3 times
Here is my code and my attached file. Please check it out:
.h
#interface myProject : UIViewController {
float alphaValue, imageAlphaValue;
}
UIImageView *IntroImage;
NSTimer *AlphaTimer;
#end
.m
-(void)viewDidLoad
{
imageAlphaValue = 0.0;
alphaValue = 0.035;
IntroImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:IntroImage];
[IntroImage setImage:[UIImage imageNamed:#"yourimagenamehere.png"]]; //use your image here
[IntroImage setBackgroundColor:[UIColor blueColor]];
[IntroImage setAlpha:imageAlphaValue];
[IntroImage release];
AlphaTimer = [NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:#selector(setAlpha) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:(2.0) target:self selector:#selector(changeAlpha) userInfo:nil repeats:NO];
}
-(void)setAlpha
{
imageAlphaValue = imageAlphaValue + alphaValue;
[IntroImage setAlpha:imageAlphaValue];
}
-(void)changeAlpha
{
alphaValue = -0.035;
}
my sample code
wow.... why you cannot just use something like that to change your image's alpha?
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationDelegate:self];
[introImage setAlpha:0.0f];
[UIView commitAnimations];
Seems to be not surprising that the main nib is shown, because it is the viewDidLoad method!
I'd also prefer to make use of the animation stuff like in morion's answer. Even though: The repeated fading is probably caused by viewDidLoad called more than once. Check it out with debugging or logging.