将ImageView.layer对象从一个类引用到另一个类

时间:2012-06-22 13:48:15

标签: iphone animation calayer

ImageView在名为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]; }

如何从第三个类到另一个类获得此图像view.layer的引用。

基本上我想使用暂停层和恢复层来进行图像视图动画。

使用

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

我应该引用这样的东西但不知道如何在第三类的viewdidload中实现它,或者在第三类或另一个类中实现它。

 UIImageView *myImageView = [OtherClass getTheImageView];

所以寻求帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

我解决了。在另一个类我加载第三类,代码在

下面
-(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];
}

我在这个加载代码中使用了相同的图层。所以我用它作为参考。

我用过

[self pauseLayer:self.view.layer];

用于暂停图像视图动画和恢复

[self resumeLayer:self.view.layer];

所以基本上在playpauseAction当我暂停并恢复计时器时我正在暂停并恢复图像view.layer动画

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

就是这么简单,我浪费了两天的时间来解决这个问题,但最后我很高兴它的工作就像魅力一样没有问题。