iOS - CAKeyFrameAnimation - 未播放

Posted

技术标签:

【中文标题】iOS - CAKeyFrameAnimation - 未播放【英文标题】:iOS - CAKeyFrameAnimation - Not Playing 【发布时间】:2011-01-21 20:48:32 【问题描述】:

我查看了一些 CAKeyFrameAnimation 指南,但我没有看到如何触发它们。我唯一能想到的是,我必须将其用作回报,但这对我来说没有多大意义。

-H文件-

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ImageSequenceViewController : UIViewController 
          <UIGestureRecognizerDelegate>

 UISwipeGestureRecognizer *swipeLeftRecognizer;


 NSMutableArray *myImages;
 IBOutlet UIImageView *imageView;
 IBOutlet UIImageView *bView;
 IBOutlet UISegmentedControl *segmentedControl;


@property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeftRecognizer;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;

-(IBAction)takeLeftSwipeRecognitionEnabledFrom:(UISegmentedControl *)aSegmentedControl;

-(IBAction)ButtonPressed1: (id)sender;

@end

-M文件-

#import "ImageSequenceViewController.h"

@implementation ImageSequenceViewController

@synthesize swipeLeftRecognizer;
@synthesize imageView;
@synthesize segmentedControl;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;

*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView 

*/

//CUSTOM CODE
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

-(void)loadLeft 
 //aView = [[UIImageView alloc] initWithFrame:self.view.frame];
 CALayer *layer = [CALayer layer];

 [layer setFrame:CGRectMake(0.0,
          0.0,
          [[self view] frame].size.height,
          [[self view] frame].size.width)];

 myImages = [[NSMutableArray alloc] init];

 for(NSUInteger count=0; count<100; count++)
  NSString *fileName;

  if (count < 10) 
  
   fileName = [NSString stringWithFormat:@"trailerRotation_000%d", count];
   
  else if (10 <= count < 100) 
  
   fileName = [NSString stringWithFormat:@"trailerRotation_00%d", count];
  
  else 
  
   fileName = [NSString stringWithFormat:@"trailerRotation_0%d", count];
  

  NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"jpg"];

  //UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
  //[myImages addObject:image];

  [myImages addObject:[UIImage imageWithContentsOfFile:path]];
 

 CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"Contents"];
 [anim setDuration:0.10];
 [anim setCalculationMode:kCAAnimationDiscrete];
 [anim setRepeatCount:1];
 [anim setValues:myImages];

 [self.view.layer addSublayer:layer];
 [layer addAnimation:anim forKey:@"images"];

 //aView.animationImages = myImages;
 //aView.animationDuration = 10.00;
 //aView.animationRepeatCount = 1;



-(void)loadRight 
 bView = [[UIImageView alloc] initWithFrame:self.view.frame];
 myImages = [[NSMutableArray alloc] init];

 for(NSUInteger count=99; count>0; count--)
  NSString *countString;

  if (count < 10) 
  
   countString = @"000";
   countString = [countString stringByAppendingFormat:@"%d", count];
   
  else if (10 <= count < 100) 
  
   countString = @"00";
   countString = [countString stringByAppendingFormat:@"%d", count];
   
  else if (100 <= count < 1000)
  
   countString = @"00";
   countString = [countString stringByAppendingFormat:@"%d", count];
  

  NSLog(@"%d", count);

  NSString *fileName = @"trailerRotation_";
  fileName = [fileName stringByAppendingFormat:countString];
  fileName = [fileName stringByAppendingFormat:@".jpg"];

  [myImages addObject:[UIImage imageNamed:fileName]];
 

 bView.animationImages = myImages; 
 bView.animationDuration = 10.00;
 bView.animationRepeatCount = 1;


- (void)viewDidLoad 
 [super viewDidLoad];

 imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
 [imageView setImage:[UIImage imageNamed:@"trailerRotation_0000.jpg"]];
 //[self.view addSubview:imageView];


 [self loadLeft];
 [self loadRight];

 UIGestureRecognizer *recognizer;

 recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
 [self.view addGestureRecognizer:recognizer];
 [recognizer release];

 recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
 self.swipeLeftRecognizer = (UISwipeGestureRecognizer *) recognizer;
 swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;

 if ([segmentedControl selectedSegmentIndex] == 0) 
  [self.view addGestureRecognizer:swipeLeftRecognizer];
 

 self.swipeLeftRecognizer = (UISwipeGestureRecognizer *) recognizer;
 [recognizer release];


- (void)viewDidUnload 
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;

 self.segmentedControl = nil;
 self.swipeLeftRecognizer = nil;
 self.imageView = nil;


- (BOOL)canBecomeFirstResponder 
    return YES;


- (void)viewDidAppear:(BOOL)animated 
    [self becomeFirstResponder];


-(void)startItLeft 
 NSLog(@"Left");

 //[aView startAnimating];
 //[self.view addSubview:aView];
 //[aView release];
 [bView release];


-(void)startItRight 
 NSLog(@"Right");

 [bView startAnimating];
 [self.view addSubview:bView];
 //[aView release];
 [bView release];


//- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 
//

//- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
//

//- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event  
//

-(IBAction)ButtonPressed1:(id)sender

 NSLog(@"Button");

 //[aView stopAnimating];
 [bView stopAnimating];
 //[self loadLeft];
 [self loadRight];


-(IBAction)takeLeftSwipeRecognitionEnabledFrom:(UISegmentedControl *) aSegmentControl 

 if ([aSegmentControl selectedSegmentIndex] == 0) 
  [self.view addGestureRecognizer:swipeLeftRecognizer];
 
 else 
  [self.view removeGestureRecognizer:swipeLeftRecognizer];
 


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *) recognizer 

 //CGPoint location = [recognizer locationInView:self.view];
 //[self showImageWithText:@"swipe" atPoint:location];

 if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) 
  //[self startItLeft];
 
 else 
  [self startItRight]; 
 

//CUSTOM CODE



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    return YES;


- (void)didReceiveMemoryWarning 
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.


- (void)dealloc 
    [super dealloc];


@end

我对 ios 开发比较陌生,所以任何建议都会有所帮助。

谢谢。

【问题讨论】:

我的建议:不要发布与您的问题无关的代码。 如果我知道我的代码中的缺陷在哪里,我会......但它会遵守并运行而没有任何控制台错误或其他指示。 仅供参考,您在-loadLeft 中创建的图层大小错误。您颠倒了宽度/高度参数的顺序。 我不相信,我设法通过将代码分块粘贴到一个新的项目文件中来使代码正常工作......当我翻牌时,我失去了大部分图像。这可能与我正在使用的图像的方向有关吗? *输不松。图片是 1024 x 768 而不是 768 x 1024。 【参考方案1】:

试试:

[myImages addObject:(id)[UIImage imageWithContentsOfFile:path].CGImage];

因为CAKeyFrameAnimation 需要CGImageRef 对象。

【讨论】:

【参考方案2】:

您似乎正在尝试加载 100 张图片并在 0.1 秒的时间内将它们全部循环播放。这不仅非常荒谬(那里是 1000fps,CoreAnimation 的上限为 60fps),而且 CoreAnimation 在开始之前复制所有图像可能需要超过 0.1 秒,这意味着它会在它之前完成已启动(因此不会出现动画)。

【讨论】:

播放速度似乎不是问题的一个因素...我已将其从 0.1 设置为 100,但它仍然在黑屏上停止,没有控制台出错。 您正在使用密钥路径@"Contents"。试试@"contents"【参考方案3】:

我承认这是一个 PEBKAC 错误。

感谢大家的帮助。

【讨论】:

以上是关于iOS - CAKeyFrameAnimation - 未播放的主要内容,如果未能解决你的问题,请参考以下文章

ios开发之--CAKeyframeAnimation的详细用法

ios开发核心动画五:图标抖动效果--CAKeyframeAnimation

iOS CoreAnimation 关键帧动画 CAKeyframeAnimation

CAAnimationGroup 与 CAKeyframeAnimation 的优缺点

如何获取CAKeyframeAnimation对象的当前屏幕xy位置?

CAKeyframeAnimation 和音频同步