Stop animation on UIScrollview - iphone

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];
}];
}

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];

how to pause and resume an uiview animation when button clicking? [duplicate]

This question already has an answer here:
How to pause and resume UIView Animation (without Block Animation)?
(1 answer)
Closed 9 years ago.
i have scrollview, which has zoomscale property inside uiview begin animation,it animates well for some duration,but in between these animations i want to pause the animation and resume the animation,please help me out of this
[UIView beginAnimations:#"anim1" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:fanim];
z = [[arrImage1 objectAtIndex:1] floatValue];
scroll.zoomScale = z;
NSLog(#" %f",scroll.zoomScale);
if (iw != 0 || ih != 0)
{
image1.frame = CGRectMake(0, 0, iw,ih);
}
z = [[arrImage2 objectAtIndex:1] floatValue];
scroll1.zoomScale = z;
z = [[arrImage3 objectAtIndex:1] floatValue];
scroll2.zoomScale = z;
[UIView commitAnimations];
That is possible by using nstimer. Try using nstimer for starting and pausing animation here is a set of code which you can use. Implement your animation within a method and use the nstimer to fire it and pause it:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self startTimer];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self stopTimer];
}
- (void)startTimer {
NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:3.0] interval:3.0 target:self selector:#selector(this is where your animation method name goest) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.animationmethodename = timer;
[timer release];
}
- (void)stopTimer {
[self.animationmethodname invalidate];
self.animationmethodtimer = nil;
}
Then replace the animationmethodname with the name of the method you are using to create the animation.

glowing effect of view disables button in it

I have uiscrollview with some buttons in it. I want to make one particular button to glow.
I made glowing-pulsating effect of button with following code:
(button in UIView)
- (IBAction)selectChampionAction:(id)sender{
if ( self.timer ) {
// Stop timer
[self.timer invalidate];
self.timer = nil;
// Workaround: we have to return the layer's delegate property to what it was
// Otherwise, we will encounter an unexpected result
self.viewForGlowing.layer.delegate = self.viewForGlowing;
} else {
// Woraround: UIView's layer property does not support animation by default
// assigning nil to layer's delegate property enables the animation somehow.
self.viewForGlowing.layer.delegate = nil;
// Start timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerExpired:) userInfo:nil repeats:YES];
[self.timer fire];
}
}
- (void) timerExpired:(NSTimer *) timer
{
if ( self.viewForGlowing.layer.shadowRadius == 20 ) {
// Increase
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:1.0f] forKey:kCATransactionAnimationDuration];
self.viewForGlowing.layer.shadowRadius = 30;
self.viewForGlowing.layer.shadowOpacity = 1;
[CATransaction commit];
} else {
// Decrease
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:1.0f] forKey:kCATransactionAnimationDuration];
self.viewForGlowing.layer.shadowRadius = 20;
self.viewForGlowing.layer.shadowOpacity = 0.7;
[CATransaction commit];
}
}
But when i press other buttons glowing button stops response.
I suppose that this happens because of this line of code
self.viewForGlowing.layer.delegate = nil;
My Questions:
Are there other approaches to make UIButton glow?
How to make button response after other buttons pressed?

Shrink UIButton

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);