1

在 h 文件的 @interface 和 @end 块中定义了 resumeLayer 和 PauseLayer 方法。

@interface MainViewController : UIViewController

@property (nonatomic, retain) UIToolbar *toolbar;
@property (strong)AVAudioPlayer *audioPlayer;
@property (nonatomic, retain) NSTimer * timer;

- (void)resumeLayer:(CALayer *)layer;
- (void)pauseLayer:(CALayer *)layer;
@end

m 文件中的方法

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

用于在 PlayPauseAction 方法中调用 resumeLayer 和 PauseLayer 方法

-(void)playpauseAction:(id)sender {

if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:ImageView.layer]; // Pause the CALayer of the UIImageView

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

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

我想在 MainViewController 类中获取此 ImageView.layer 的引用以用于

  [self pauseLayer:ImageView.layer];

暂停 imageview.layer 动画

现在我怎样才能得到这个 ImageView 的参考使用

  UIImageView *myImageView = [OtherClass getTheImageView];

在 playpauseAction 中使用以下来暂停和恢复 Imageview 层动画

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

任何人都可以帮助我这个如何在 MainviewController 中引用 ImageView.layer

4

1 回答 1

1

要么添加一个属性MainviewControllerImageView在它准备好时设置它的值,要么使用 NSNotificationCenter 将通知从 MainViewController 发送到 ImageView;

ImageView

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseMyLayer) name:kPauseLayerNotif object:nil];

MainViewController

[[NSNotificationCenter defaultCenter] postNotificationName:kPauseLayerNotif object:self];

通知机制为您提供了两个类之间非常松散的耦合,并且非常易于使用。

于 2012-06-21T18:26:36.877 回答