开发者

iOS - CAKeyFrameAnimation - Not Playing

I have looked at a few guides for CAKeyFrameAnimation and I am failing to see how to trigger them. The only thing I can think of is that I have to use it as a return, but that doesn't make much sense to me.

-H File-

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

@interface ImageSequenceViewController : UIViewController 
          <UIGestureRecognizerDelegate>{

 UISwipeGestureRecognizer *swipeLeftRecognizer;


 NSMutableArray *myImages;
 IBOutlet UIImageView *imageView;
 IBOutlet UIImageView *bView;
 IBOutlet UISegmentedControl *seg开发者_JAVA技巧mentedControl;
}

@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 File-

#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

I'm rather new to iOS development so any advice is helpful.

Thanks.


Try:

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

Since the CAKeyFrameAnimation expects CGImageRef objects.


It looks like you're trying to load up 100 images and cycle them all within the space of 0.1 second. Not only is that pretty ridiculous (that's 1000fps right there, and CoreAnimation is capped at 60fps), but it's likely to take longer than 0.1 seconds for CoreAnimation to copy all the images before it even starts, which means that it will have finished before it started (and therefore no animation will occur).


I'll admit it was a PEBKAC error.

Thanks all for your help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜