-1

Hi I am creating a new class CNAnimations. The purpose of this class is to perform some simple animations with ease and without using the same code a lot of times. So, I would like it to behave like [UIView animateWithDuration...]. Should I create a singleton class and init it in the appdelegate and the access it: [CNAnimations moveRight:view] ? Is that correct? Please note that the class will be used by a lot of uilabels,textfields etc from different classes.

Simple code:

  -(void)moveRL:(UIView*)view{

CAKeyframeAnimation * anim = [ CAKeyframeAnimation animationWithKeyPath:@"transform" ] ;
anim.values = @[ [ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-5.0f, 0.0f, 0.0f) ], [ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(5.0f, 0.0f, 0.0f) ] ] ;
anim.autoreverses = YES ;
anim.repeatCount = 2.0f ;
anim.duration = 2.0f ;

[view.layer addAnimation:anim forKey:nil ] ;

}

rmaddy
  • 314,917
  • 42
  • 532
  • 579
BlackM
  • 3,927
  • 8
  • 39
  • 69

2 Answers2

3

Since you don't seem to need an object of 'CNAnimations' and it serves more or less as a proxy for kicking off animations, you could simply define all methods at class scope (writing '+' instead of '-'). You can then do [CNAnimation animateWhatever:] without the need to keep a singleton.

CodingMeSwiftly
  • 3,231
  • 21
  • 33
1

Objective-C classes are also an opaque type called Class. This allows you to not have to allocate memory and initialize a class to use a classes factory methods. Factory methods are defined using + instead of -. for example, +(id)moveRL you often use factory methods without realizing it, such as NSString text = [NSString stringWithFormat:"@Hello World!"] The stringWithFormat is the factory method in this example.

urnotsam
  • 770
  • 7
  • 24