我看了一些关于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开发的新手,所以任何建议都对您有所帮助。

谢谢。

最佳答案

尝试:

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


由于CAKeyFrameAnimation需要CGImageRef对象。

关于iphone - iOS-CAKeyFrameAnimation-无法播放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4763657/

10-10 17:35