Connected rewindButton with playButton programmatically. rewindButton is simply rewinding and playing from the beginning and playButton toggles from play to pause.In the rewindButton method used statement to change the UIImage of UIButton2 when UIButton1 is pressed. That is working fine.
PlayButton method is working fine when play button is pressed and paused. Even after resuming is working fine.
Issue briefing:
When rewindButton is pressed it will change the UIImage of playButton to PauseImage so that user can pause. Problem arises when pause is resumed. When pause is resumed it is reloading view controllers.It should load viewcontrollers only for the first time as per code.
-(void)rewind:(id)sender{
audioPlayer.currentTime = 0;
[timer invalidate];
ContainerViewController *viewController = [[[ContainerViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
[_playButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
}
-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:self.view.layer];
}else{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
[self resumeLayer:self.view.layer];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
}
}
- (void)displayviewsAction:(id)sender
{
First *first = [[First alloc] init];
first.view.frame = CGRectMake(0, 0, 320, 425);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:first.view];
[first release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
}
-(void)Second
{
Second *second = [[Second alloc] init];
second.view.frame = CGRectMake(0, 0, 320, 425);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:second.view];
[second release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
Reason it was reloading view controllers after resuming cause in the rewind method i forgot to add isFirstTime == YES for loading view controllers. Viewcontrollers are not required to reload. Viewcontrollers are required to start loading from the beginning only for the first time only.
So after setting the value of isFirstTime == YES in rewind method now it is not reloading view controllers from the beginning after it is resumed from the pause.
-(void)rewind:(id)sender{
audioPlayer.currentTime = 0;
[timer invalidate];
ContainerViewController *viewController = [[[ContainerViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[audioPlayer play];
if(isFirstTime == YES){
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
[_playButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
}
Now it is working fine.
Related
Loading multiple view controllers one after the other with delay like this
-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:self.view.layer];
}else{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
[self resumeLayer:self.view.layer];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}}}
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
}
-(void)Second
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
Requirement is to add a container view and load all view controllers in containerview.
So, in the load view can create uiview and container view like this
- (void)loadView
{
// set up the base view
CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:frame];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
view.backgroundColor = [UIColor blueColor];
// set up content view a bit inset
frame = CGRectInset(view.bounds, 0, 100);
_containerView = [[UIView alloc] initWithFrame:frame];
_containerView.backgroundColor = [UIColor redColor];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[view addSubview:_containerView];}
In the AppDelegate can do it like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ContainerViewController *container = [[ContainerViewController alloc] init];
self.window.rootViewController = container;
// make an array of 23 PageVCs
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:firstcontroller];
[controllers addObject:secondcontroller];
[controllers addObject:.....];
[controllers addObject:twentythreecontroller];
for (int i=0; i<23; i++)
{
First *firstController = [[First alloc] init];
[controllers addObject:firstcontroller, secondcontroller, .....,twentythreecontroller];
}
// set these as sub VCs
[container setSubViewControllers:controllers];
[self.window makeKeyAndVisible];
return YES;}
Loading all viewcontrollers in container view like this will be okay or doing something wrong in here.
Appreciate help for this.
Thanks.
Firstly remove nstimer and use performSelector:withObject:afterDelay: method like this:
// add delay and method according to your requirement
[self performSelector:#selector(oneView) withObject:nil afterDelay:10];
-(void)oneView
{
//add firstView and perform second selector
[self performSelector:#selector(secondView) withObject:nil afterDelay:10];
}
-(void)secondView
{
//add secondView and perform thirdselector
[self performSelector:#selector(thirdView) withObject:nil afterDelay:10];
}
goes on .......................
I want to pause UIImageView animation and based on my research found that you can stop the animation of the images but you cannot pause the sequence. By calling statement stop animating on the UIImageView then it stops the animation and blanks the image view.
To pause the animation of UIImages one has to use NSTimer.
I m already using one NSTimer in app for loading view controllers one after the other at different time intervals. I have a slide show of UIImages in each view controller.
Issue is when Timer is paused view controllers are paused from loading further but the view controller which is on display at that time still shows the slide show of UIImages of that view controller repeatedly until pause is resumed. So I want to pause that slideshow too.
This is where i m using the NSTimer
-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
}else{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
}
}
This is how displayviewsAction loads 11 view controllers one after another at different time intervals
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
}
-(void)Second
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
And that is how starting image animation within the view controller
// Displays UIImageView
UIImageView* ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 300, 235)];
self.view.backgroundColor = [UIColor brownColor];
// load all the frames of our animation
ImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1a.png"],
[UIImage imageNamed:#"1b.png"],
nil];
// all frames will execute in 25 seconds
ImageView.animationDuration = 25;
// start animating
[ImageView startAnimating];
ImageView.layer.borderWidth = 2;
ImageView.layer.borderColor = [[UIColor whiteColor]CGColor];
[ImageView.layer setMasksToBounds:YES];
[ImageView.layer setCornerRadius:15.0f];
[baseView addSubview:ImageView];
Now if someone can guide me how to pause this slideshow of UIImage animation when this NSTimer pauses.
Thanks for help.
you can use the code provided by apple to pause and resume your animation.
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
And to get the CALayer to pass to the pauseLayer function, you can use the following code.
CALayer *player = ImageView.layer;
[self pauseLayer:player];
Hope it helps you.
Declared variable in m file.
BOOL isFirstTime;
I want that if play button is pressed the very first time then it should toggle to pause button and check the function isFirstTime then it should start the timer and execute the displayviewsAction method and if the pause button is resumed to play back then it should check again the function isFirstTime or second time and if the hit is for the second time then it should resume timer.
-(void)playpauseAction:(id)sender {
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(AfterOneSecondsFuction) userInfo:nil repeats:YES];
}
}
-(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
-(void)AfterOneSecondsFuction
{
if(!isFirstTime)
{
isFirstTime=YES;
return;
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
} else if (!isFirstTime){
isFirstTime=NO;
return;
[self resumeTimer];
}
}
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
}
-(void)Second
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
Looks like when play is pressed first it is only toggling to pause button not checking the function isFirstTime or not and not starting the timer and executing displayviewsaction.
If anyone can tell why it is not happening and what i m doing wrong.
Thanks for help.
BOOL isFirstTime
//Somewhere where the object is set up, isFirstTime should be set to YES
- (void)playpauseAction:(id)sender {
//When the button is pressed
if(isFirstTime) {
//Load the sound file
//Start playing the sound
//Start view controller timers
//Change the button image to paused
isFirstTime = FALSE;
}
else {
//The button has already been pressed
if ([audioPlayer isPlaying]) {
//If the sound is playing
//Stop playing the sound
//Stop view controller timers
//Change the button image to playing
}
else {
//Resume playing the sound
//Start view controller timers
//Change the button image to paused
}
}
}
I don't know how to code this but i want program to skip or jump this statement from executing
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
when pause is pressed to resume play.
Reason i want this cause in play/pause button everything is good except when pause is pressed to resume it starts reloading view controllers by executing this statement from the below code
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
I have tried so many different ways to solve this issues but nothing is working out.
if([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
}else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
That's how loading view controllers one after other
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
}
-(void)Second
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
This is my last attempt to see if i can find some solution for this issue. I have spent two weeks on this issue but no success.
If someone can solve my this issue.
Thanks so much for help.
I found one little bit complex solution.
In you .h file create a BOOL isFirstTime; int methodSelector;.
In your implementation file:
-(void)viewDidLoad
{
isFirstTime = YES;
methodSelector = 1;
}
and change your code like this:
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
}
else
{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
switch(methodSelector)
{
case 1:self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
break;
case 2:self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
break;
.
.
.
}
}
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
set the methodSelector in your view changing methods accordingly. In Second method set it as 2 in Third set it as 3 and so on.
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
methodSelector = 2;
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
}
-(void)Second
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
methodSelector = 3;
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
methodSelector is used for calling the method after pausing.
I want to add CATransition to UIViewControllers which i am displaying using NSTimer
-(void)playAction:(id)sender
{
[audioPlayer play];
[self performSelector:#selector(displayviewsAction:) withObject:nil afterDelay:11.0];
}
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(Second) userInfo:nil repeats:NO];
}
-(void)Second
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
How i can add CATransition to these UIViewControllers when they change from first to second and so on.
Thanks in advance for all ideas.
Use this -
-(void)Second {
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(Third) userInfo:nil repeats:NO];
}
// you can define your own CATransition animation type in this.