Iphone iOS-动画-不播放

Iphone iOS-动画-不播放,iphone,objective-c,ipad,ios,uiview,Iphone,Objective C,Ipad,Ios,Uiview,我已经看了一些关于CAKeyFrameAnimation的指南,但我没有看到如何触发它们。我唯一能想到的是,我必须用它作为回报,但这对我来说没有多大意义 -H文件- #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface ImageSequenceViewController : UIViewController <UIGestureRecognizerDele

我已经看了一些关于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
#导入
#进口
@接口ImageSequenceViewController:UIViewController
{
UISweepGestureRecognizer*swipeLeftRecognizer;
NSMutableArray*myImages;
IBUIImageView*imageView;
IBUIImageView*bView;
IBUI分段控制*分段控制;
}
@属性(非原子,保留)UISwipeGestureRecognizer*swipeLeftRecognizer;
@属性(非原子,保留)UIImageView*imageView;
@属性(非原子,保留)IBUI分段控件*分段控件;
-(iAction)从:(UISegmentedControl*)AseSegmentedControl中获取LeftsWipereCognitionEnabled;
-(iAction)按钮按下1:(id)发送者;
@结束
-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
#导入“ImageSequenceViewController.h”
@ImageSequenceViewController的实现
@合成Swipeleftre识别器;
@综合图像视图;
@综合分段控制;
/*
//指定的初始值设定项。覆盖以执行加载视图之前所需的设置。
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
*/
/*
//实现loadView以编程方式创建视图层次结构,而不使用nib。
-(void)负荷视图{
}
*/
//自定义代码
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)左上角{
//aView=[[UIImageView alloc]initWithFrame:self.view.frame];
CALayer*层=[CALayer层];
[layer setFrame:CGRectMake(0.0,
0.0,
[[self view]frame].size.height,
[[self view]frame].size.width];
myImages=[[NSMutableArray alloc]init];

对于(NSUInteger count=0;count,看起来您正试图加载100幅图像,并在0.1秒内将它们全部循环。这不仅是非常荒谬的(这是1000帧/秒,CoreAnimation的上限是60帧/秒),但CoreAnimation在开始之前复制所有图像可能需要超过0.1秒的时间,这意味着它将在开始之前完成(因此不会出现动画).

看起来你正试图加载100幅图像,并在0.1秒内将它们全部循环。这不仅是非常荒谬的(这是1000帧/秒,CoreAnimation的上限是60帧/秒),但CoreAnimation在开始之前复制所有图像可能需要超过0.1秒的时间,这意味着它将在开始之前完成(因此不会出现动画)。

我承认这是一个PEBKAC错误


谢谢大家的帮助。

我承认这是PEBKAC的错误

谢谢大家的帮助。

试试:

[myImages addObject:(id)[UIImage imageWithContentsOfFile:path].CGImage];
因为
cakeyKeyMeanimation
需要
CGImageRef
对象。

请尝试:

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

因为
CAKeyFrameAnimation
需要
cImageRef
对象。

我的建议是:不要发布与您的问题无关的代码。如果我知道代码中的缺陷在哪里,我会……但它符合并没有任何控制台错误或其他指示。仅供参考,您在
-loadLeft
中创建的层大小不正确。哟u翻转了宽度/高度参数的顺序。我不这么认为,我通过将代码分块粘贴到一个新的项目文件中,成功地使代码工作……当我翻动这些数字时,我丢失了大部分图像。这可能与我正在使用的图像的方向有关吗?*不要丢失。图像是1024 x 768,而不是768 x 1024。我的建议:不要发布与您的问题不相关的代码。如果我知道代码中的缺陷在哪里,我会……但它符合要求,并且运行时没有任何控制台错误或其他指示。仅供参考,您在
-loadLeft
中创建的层的大小不正确。您翻转了宽度/高度参数的顺序。我不相信,我成功地将代码设置为通过将其粘贴到一个新的项目文件中,分块进行标记…当我翻动这些数字时,我丢失了大部分图像。这可能与我正在使用的图像的方向有关吗?*不要丢失。图像是1024 x 768,而不是768 x 1024。它的播放速度似乎不是问题的一个因素…我从0.1设置它到100,它仍然会在黑屏上暂停,控制台中没有错误。您正在使用键路径
@“Contents”
。请尝试
@“Contents”
。它的播放速度似乎不是它出现问题的一个因素…我将它从0.1设置为100,但它仍然会在黑屏上暂停,控制台中没有错误。您正在使用键路径
@“Contents”
。请尝试
@“Contents”