在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
答案 0 :(得分:1)
要么将属性添加到MainviewController
并让ImageView
在其准备就绪时设置其值,要么使用NSNotificationCenter从MainViewController向ImageView发送通知;
在ImageView
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseMyLayer) name:kPauseLayerNotif object:nil];
在MainViewController
:
[[NSNotificationCenter defaultCenter] postNotificationName:kPauseLayerNotif object:self];
通知机制为您提供了两个类之间非常松散的耦合,并且它非常易于使用。