Search code examples
iphoneanimationcalayer

Reference of ImageView.layer object from one class to another


ImageView is defined in class named Third

- (void)viewDidLoad
{
// 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:@"3a.png"],
                         [UIImage imageNamed:@"3b.png"],
                         nil];
// all frames will execute in 24 seconds
ImageView.animationDuration = 24;
// start animating
[ImageView startAnimating];
 ImageView.layer.borderWidth = 2;
 ImageView.layer.borderColor = [[UIColor whiteColor]CGColor];    
 [ImageView.layer setMasksToBounds:YES];
 [ImageView.layer setCornerRadius:15.0f];
 [self.view addSubview:ImageView]; }

How can i get reference of this image view.layer from this third class to another class.

Basically i want to use pause layer and resume layer for image view animation.

To use

 [self pauseLayer:myImageView.layer]; // Pause the CALayer of the UIImageView

I should have reference some thing like this but don't know how to implement it in viewdidload of third class or where to implement it in third class or in another class.

 UIImageView *myImageView = [OtherClass getTheImageView];

So looking for some help.

Thanks.


Solution

  • I solved it. In another class i was loading third class and the code is below

    -(void)Third {
    Third *thirdController = [[Third alloc] init];
    thirdController.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:thirdController.view]; 
    [self.view addSubview:toolbar];
    [thirdController release];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:@selector(Fourth) userInfo:nil repeats:NO];
    }
    

    i was using same layer in this loading code. So i used that for reference.

    I used

    [self pauseLayer:self.view.layer];
    

    for pausing image view animation and for resuming

    [self resumeLayer:self.view.layer];
    

    So basically in playpauseAction when i m pausing and resuming timer right their i am pausing and resuming image view.layer animation

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

    It was that simple and i wasted my two days to solve this but at last happy that it is working like charm have no issues.