2013-08-05 3 views
0

У меня есть контроллер просмотра, где я настроил UIImageView и хочу оживить его с помощью QuartzCore в Xcode. Я построил UIImage для работы в режиме просмотра изображения, который вращается. Это вращается отлично, когда я тестировал его в своем собственном проекте, но теперь, когда я втягиваю его в свой текущий рабочий проект, он добавляет изображение, но не оживляет? Не знаю, почему? Есть идеи?QuartzCore Not Animated?

У меня нет ошибок или предупреждений, и это тот же код, который работал в отдельном проекте? Я также добавил библиотека QuartzCore и #imported

- (void)viewDidLoad { 


    //Setup Image View 
    UIImage *image = [UIImage imageNamed:@"record01.png"]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
    [imgView setFrame: CGRectMake(0, 0, image.size.width, image.size.height)]; 
    [imgView setCenter:(CGPoint){160,160}]; 
    [self.view addSubview:imgView]; 

    //Animate Image View 
    CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    fullRotation.fromValue = [NSNumber numberWithFloat:0]; 
    fullRotation.toValue = [NSNumber numberWithFloat:2 * M_PI]; 
    fullRotation.duration = 1.5; 
    fullRotation.repeatCount = HUGE_VALF; 
    [imgView.layer addAnimation:fullRotation forKey:@"fullRotation"]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

} 

консоли для ро [[UIApp keyWindow] recursiveDescription]

| | <UIImageView: 0x984a9f0; frame = (-6.5 -6.5; 333 333); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x984a040>> 
(lldb) po 0x984a040 
(int) $2 = 159686720 <CALayer:0x984a040; position = CGPoint (160 160); bounds = CGRect (0 0; 333 333); delegate = <UIImageView: 0x984a9f0; frame = (-6.5 -6.5; 333 333); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x984a040>>; contents = <CGImage 0x9843560>; rasterizationScale = 2; contentsScale = 2> 
(lldb) 
+1

'[imgView setFrame: (CGRect) {{0,0}, image.size}];': этот синтаксис плохой форме, но совершенно не нужны. 'UIImageView' по умолчанию будет иметь размер изображения. Предпочтительный (но ненужный): 'CGRectMake (0, 0, image.size.width, image.size.height);') – bshirley

+0

Спасибо @bshirley - я изменил код, чтобы это отразить. Любая идея о том, почему она не оживляет меня? –

+0

Я не согласен с тем, что использование синтаксиса C99 - это плохая форма. На самом деле я использую его исключительно. Может быть, это просто разница во мнениях? Почему предпочтительнее «CGRectMake»? – nielsbot

ответ

0

Я хотел бы остаться из CoreAnimation, если то, что вы можете получить на более высокий уровень будет делать трюк:

- (void)spinOnce:(UIView *)view { 
    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI * 2); 

    [UIView animateWithDuration:1.5 
        animations:^{ 
        view.transform = rotate; 
        } completion:^(BOOL finished) { 
        view.transform = CGAffineTransformIdentity; 
        [self spinOnce:view]; 
        }]; 
} 

- (void)viewDidLoad { 

    //Setup Image View 
    UIImage *image = [UIImage imageNamed:@"record01.png"]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
    [imgView setFrame: (CGRect){{0,0},image.size}]; 
    [imgView setCenter:(CGPoint){160,160}]; 
    [self.view addSubview:imgView]; 

    //Animate Image View  
    [self spinOnce:imgView]; 

// CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
// fullRotation.fromValue = [NSNumber numberWithFloat:0]; 
// fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)]; 
// fullRotation.duration = 1.5; 
// fullRotation.repeatCount = HUGE_VALF; 
// [imgView.layer addAnimation:fullRotation forKey:@"360"]; 
// 
// [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

} 
+0

Спасибо @bshirley - У меня есть это там успешно, но он все еще не оживляет? Я не знаю, что может пойти не так? Может быть, я показываю его в раскадровке, и мне не хватает какой-то супер простой вещи? –

+2

'CGAffineTransformMakeRotation (M_PI * 2)' возвращает единичную матрицу. Вы не должны ожидать, что это что-то сделает. –

+0

(я не тестировал это специально, но использовал аналогичный код раньше), попробуйте 359 °, если 360 ° не работает, или даже разделить его пополам: Идентичность/0 ° до 180 ° и от 180 ° до 0 °. – bshirley