0

I copied this tutorial; https://www.youtube.com/watch?v=uTbi_0LFxm0

I think the code is deprecated because the vid was posted 4 years ago.Especially because it used the old 'appdidfinishlaunching' method. Is this workable?

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
//this was a uiwindow in .h
@synthesize whatsup=whatsup;



- (void)viewDidLoad
 {

UIImage * image = [UIImage imageNamed:@"sexy.jpg"];

UIImageView * imgView = [[UIImageView alloc]initWithImage:image];

[imgView setFrame:(CGRect){{0,0},image.size}];

[imgView setCenter:(CGPoint){160,240}];

[whatsup addSubview:imgView];

CABasicAnimation * fullRotation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];

fullRotation.fromValue=[NSNumber numberWithFloat:0];

fullRotation.toValue=[NSNumber numberWithFloat:((360*M_PI)/180)];

fullRotation.duration = 6;

fullRotation.repeatCount= 1;


[imgView.layer addAnimation:fullRotation forKey:@"360"];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Nothing really loads. Should i just nix it? Do you think this works?

UIView Infinite 360 degree rotation animation?

Community
  • 1
  • 1
user3720631
  • 53
  • 1
  • 10
  • 2
    Look into the documentation. Are the classes or methods being used marked as deprecated in relation to the Deployment Target you selected for your project? As a rule, using a 4-year old tutorial for APIs that change every year can lead to issues. – rmaddy Jul 13 '14 at 04:23
  • 1
    build the code, Xcode will tell you if it using any deprecated API – Bryan Chen Jul 13 '14 at 04:29

1 Answers1

2

I will answer this question the best I can ; please honor us with the favor of considering the advice that follows.

  1. No, nothing is deprecated.
  2. It is poorly written, but that is beside the point.
  3. Ensure your image "sexy.jpg" is part of the executable. A simple way to do that is to add NSParameterAssert(image); after loading it.
  4. [super viewDidLoad]; is the 1st thing you should do, not the last.

Now for the advice.

StackOverflow is not there to blog about pieces of code scraped from other sites, but rather discuss specific and well defined topics.

As such, your question could read:

  1. is CABasicAnimation deprecated in iOS 7? Answer: no.
  2. is this -viewDidLoad properly implemented? Answer: no.
  3. what are my defensive options to debug this? Answer: step through the debugger, verify all structures and pointers, etc.

Finally, here is my interpretation of the code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage * image = [UIImage imageNamed:@"test.png"];
    NSParameterAssert(image);

    UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
    [imgView setCenter:self.view.center];
    [self.view addSubview:imgView];

    CABasicAnimation * fullRotation=[CABasicAnimation
                                     animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue=@(0);
    fullRotation.toValue=@(2*M_PI);
    fullRotation.duration = 6;
    fullRotation.repeatCount= 1;

    [imgView.layer addAnimation:fullRotation forKey:@"360"];
}
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • Hey Lancelot de la Mare; Is that code spinning the whole view or just the image? Also is there a way to anchor the spin point? – user3720631 Jul 16 '14 at 02:37
  • (1a) Spin the **image**: `[imgView.layer addAnimation...`. (1b) To spin the **whole view**, use `[self.view.layer addAnimation:...`. (2) To anchor the animation, you need to **anchor the layer** itself. For example `imgView.layer.anchorPoint = CGPointZero;` – SwiftArchitect Jul 16 '14 at 02:56
  • -(IBAction)segue:(segue?)sender – user3720631 Jul 20 '14 at 23:18