Más contenido relacionado Strategy Pattern for Objective-C6. Application
Warrior * michael = [Warrior new];
michael.weapon = [Sword new];
[michael attack];
michael.weapon = [Staff new];
[michael attack];
13年9月1⽇日星期⽇日
7. Codes-Warrior
@interface Warrior : NSObject
@property Weapon * weapon;
-(void) attack;
@end
@implementation Warrior
-(void) attack{
[self.weapon behavior];
}
@end
13年9月1⽇日星期⽇日
8. Codes-Weapon
@interface Weapon : NSObject
-(void) behavior;
@end
@implementation Weapon
@end
@interface Sword : Weapon
@end
@implementation Sword
-(void) behavior{
NSLog(@"Slash...");
}
@end
13年9月1⽇日星期⽇日
9. Codes-Weapon
@interface Weapon : NSObject
-(void) behavior;
@end
@implementation Weapon
@end
@interface Staff : Weapon
@end
@implementation Staff
-(void) behavior{
NSLog(@"Fire Ball");
}
@end
13年9月1⽇日星期⽇日
11. Purpose
• Let the subclass must implement this method
• Avoid warning
13年9月1⽇日星期⽇日
16. CardView
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
-(void) changeImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年9月1⽇日星期⽇日
17. Add effect in the class
- (void)flipOver:(NSString *) direction {
CATransition* transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn];
transition.duration = 0.5f;
transition.type = @"flip";
transition.subtype = direction;
[self.layer removeAllAnimations];
[self.layer addAnimation:transition forKey:kCATransition];
}
-(void) showFront{
if (self.isLocked) {
return;
}
[self flipOver:@"fromLeft"];
self.image = self.frontImage;
self.isFront = YES;
}
Core Animation
13年9月1⽇日星期⽇日
18. Extract what into a class ?
- (void)flipOver:(NSString *) direction {
CATransition* transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn];
transition.duration = 0.5f;
transition.type = @"flip";
transition.subtype = direction;
[self.layer removeAllAnimations];
[self.layer addAnimation:transition forKey:kCATransition];
}
should be related to the view object
13年9月1⽇日星期⽇日
19. FlipEffect - Class
@interface FlipEffect : NSObject
@property (weak) UIView * view;
-(void) flipToRight;
-(void) flipToLeft;
@end
@implementation FlipEffect
- (void)flipOver:(NSString *) direction {
// ignore
[self.view.layer removeAllAnimations];
[self.view.layer addAnimation:transition forKey:kCATransition];
}
-(void) flipToRight{
[self flipOver:@"fromLeft"];
}
-(void) flipToLeft{
[self flipOver:@"fromRight"];
}
@end
13年9月1⽇日星期⽇日
20. CardView - Modification
@class FlipEffect;
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
@property FlipEffect * effect;
-(void) changeImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年9月1⽇日星期⽇日
21. CardView.m
-(void) showFront{
if (self.isLocked) {
return;
}
[self.effect flipToRight];
// [self flipOver:@"fromLeft"];
self.image = self.frontImage;
self.isFront = YES;
}
-(void) showBack{
if (self.isLocked) {
return;
}
[self.effect flipToLeft];
self.image = self.backImage;
self.isFront = NO;
}
13年9月1⽇日星期⽇日
27. Create ExchangeEffect - Interface
@interface ExchangeEffect : NSObject
@property (weak) UIView * view;
-(id) initWithView:(UIView *) targetView;
-(void) action;
-(void) reverseAction;
@end
@implementation ExchangeEffect
-(void) action{
[self doesNotRecognizeSelector:_cmd];
}
-(void) reverseAction{
[self doesNotRecognizeSelector:_cmd];
}
@end
13年9月1⽇日星期⽇日
28. FlipEffect
@interface FlipEffect : ExchangeEffect
@end
@implementation FlipEffect
-(void) action{
[self flipToRight];
}
-(void) reverseAction{
[self flipToLeft];
}
- (void)flipOver:(NSString *) direction {
//....
}
-(void) flipToRight{
[self flipOver:@"fromLeft"];
}
-(void) flipToLeft{
[self flipOver:@"fromRight"];
}
@end
13年9月1⽇日星期⽇日
29. CardView.h
@class ExchangeEffect;
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
@property ExchangeEffect * effect;
-(void) changeImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年9月1⽇日星期⽇日
30. CardView.m
-(void) showFront{
if (self.isLocked) {
return;
}
[self.effect action];
self.image = self.frontImage;
self.isFront = YES;
}
-(void) showBack{
if (self.isLocked) {
return;
}
[self.effect reverseAction];
self.image = self.backImage;
self.isFront = NO;
}
13年9月1⽇日星期⽇日
31. Use object
[self.cards enumerateObjectsUsingBlock:^(id obj,
NSUInteger idx, BOOL *stop) {
CardView * cardView = obj;
ExchangeEffect * effect = nil;
if (idx %2 == 0) {
effect =[FlipEffect new];
cardView.effect = effect;
effect.view = cardView;
}else{
effect = [WaveEffect new];
cardView.effect = effect;
effect.view = cardView;
}
}];
13年9月1⽇日星期⽇日