SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
Play cards
Michael
13年8月2⽇日星期五
Problem
• http://bit.ly/18T5ns4
13年8月2⽇日星期五
Basic Requirements
• http://bit.ly/18T5X9d
13年8月2⽇日星期五
Observe the behaviors of objects
13年8月2⽇日星期五
View changes
CardView
showFront
showBack
lock
13年8月2⽇日星期五
CardView - Class
CarView
showFront
showBack
lock
13年8月2⽇日星期五
CardView - Objective-C
@interface CardView : UIImageView
-(void) showFront;
-(void) showBack;
-(void) lock;
@end
13年8月2⽇日星期五
showFront - think
• what is this meaning in UIImageView ?
• UIImageView has a property named image
• what is the implementation of show front
13年8月2⽇日星期五
showFront - implementation
@implementation CardView
-(void) showFront{
self.image = self.frontImage;
}
@end Do not have this property
13年8月2⽇日星期五
Add new property
CarView
frontImage
frontImage: UIImage
setFrontImage_(UIImage * newImage):void
showFront
showBack
lock
13年8月2⽇日星期五
Objective-C codes
@interface CardView : UIImageView
@property UIImage * frontImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
@end
13年8月2⽇日星期五
The same policy for backImage
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
@end
13年8月2⽇日星期五
Testing
• test showFront
• test showBack
13年8月2⽇日星期五
Testing Code - test showFront
-(void) testShowFront{
CardView * card = [CardView new];
card.frontImage = [UIImage imageNamed:@"front0.png"];
[card showFront];
if( ![card.frontImage isEqual:card.image]){
NSLog(@”show front not works”);
}
}
13年8月2⽇日星期五
Testing Code - test showBack
-(void) testShowBack{
CardView * card = [CardView new];
card.backImage = [UIImage imageNamed:@"back.png"];
[card showBack];
if( ![card.backImage isEqual:card.image]){
NSLog(@”show back not works”);
}
}
13年8月2⽇日星期五
Refactor
-(void) testShowFront{
[self setUp];
//...
}
-(void) testShowBack{
[self setUp];
//...
}
-(void) setUp{
card = [CardView new];
}
@interface CardView{
CardView * card;
}
@end
13年8月2⽇日星期五
Demo
13年8月2⽇日星期五
Consider Lock/Unlock
CardView
lock
unlock
13年8月2⽇日星期五
Add unlock
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年8月2⽇日星期五
lock/Unlock - implementation
- (void)lock {
self.isLocked = YES;
self.layer.borderColor = [[UIColor blueColor]CGColor];
self.layer.borderWidth = 5.0;
}
- (void)unlock {
self.isLocked = NO;
self.layer.borderColor = [[UIColor blackColor]CGColor];
self.layer.borderWidth = 1.0;
}
change border colorisLocked ??????
13年8月2⽇日星期五
Add private property
• in CardView.m
@interface CardView()
@property BOOL isLocked;
@end
@implementation CardView
// ignored...
@end
13年8月2⽇日星期五
Lock behavior
• when lock, disable showFront or showBack
13年8月2⽇日星期五
showFront/back - updated
@implementation CardView
-(void) showFront{
if(self.isLocked)
return;
self.image = self.frontImage;
}
@end
lock behavior
13年8月2⽇日星期五
Tests
-(void) testShowFrontWhenLock{
[self setUp];
card.frontImage = [UIImage imageNamed:@"front0.png"];
card.backImage = [UIImage imageNamed:@"back.png"];
[card showFront];
[card lock];
[card showBack];
if( ![card.frontImage isEqual:card.image]){
NSLog(@”lock not works”);
}
}
13年8月2⽇日星期五
Continue to solve the problem
control all card objects
13年8月2⽇日星期五
IBOutletCollection - NSArray * cards
13年8月2⽇日星期五
Call the method on all objects in array
[self.cards makeObjectsPerformSelector:@selector(setBackImage:)
withObject:[UIImage imageNamed:@"back"] ];
CardView CardView CardView CardView CardView
[cardView setBackImage:[UIImage imageNamed:@”back”]]
cards
13年8月2⽇日星期五
- (void)prepareImages
{
[self.cards makeObjectsPerformSelector:@selector(setBackImage:)
withObject:[UIImage imageNamed:@"back"] ];
[self.cards enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,
BOOL *stop) {
CardView * cardImage = obj;
cardImage.frontImage = [UIImage imageNamed:[NSString
stringWithFormat:@"front%d.png",idx]];
}];
[self.cards makeObjectsPerformSelector:@selector(showBack) ];
}
Prepare data
all card have back image
set front image
13年8月2⽇日星期五
Change Images
13年8月2⽇日星期五
Change Images
change Image
13年8月2⽇日星期五
Change Images
change Image
13年8月2⽇日星期五
Class Diagram - Add new method
CarView
+frontImage
+backImage
-isLocked
showFront
showBack
lock
unlock
changeImage
13年8月2⽇日星期五
Objective-C - implementation
-(void) changeImage{
if (self.isLocked) {
return;
}
if (self.isFront) {
[self showBack];
}else{
[self showFront];
}
}
13年8月2⽇日星期五
Add new property
CarView
+frontImage
+backImage
-isLocked
-isFront
showFront
showBack
lock
unlock
changeImage
13年8月2⽇日星期五
Private property
@interface CardView()
@property BOOL isLocked;
@property BOOL isFront;
@end
13年8月2⽇日星期五
Modified Related methods
• showFront
• showBack
13年8月2⽇日星期五
show front & show back
-(void) showFront{
if (self.isLocked) {
return;
}
self.image = self.frontImage;
self.isFront = YES;
}
-(void) showBack{
if (self.isLocked) {
return;
}
self.image = self.backImage;
self.isFront = NO;
}
13年8月2⽇日星期五
IBAction
13年8月2⽇日星期五
Demo
13年8月2⽇日星期五
Lets lock
Segmented Control
Touch
or
Get the object
[cardView lock]
13年8月2⽇日星期五
Segmented Control
0 1 2 3 4
CardView CardView CardView CardView CardView
cards
13年8月2⽇日星期五
Segmented Control
0 1 2 3 4
CardView CardView CardView CardView CardView
cards
13年8月2⽇日星期五
Segmented Control
0 1 2 3 4
CardView CardView CardView CardView CardView
cards
13年8月2⽇日星期五
Segmented Control
0 1 2 3 4
CardView CardView CardView CardView CardView
cards
[cardView lock]
13年8月2⽇日星期五
Touch
• CardView.m
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.isLocked) {
[self unlock];
}else{
[self lock];
}
}
13年8月2⽇日星期五
Demo
13年8月2⽇日星期五

Más contenido relacionado

Más de Michael Pan

Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android StudioMichael Pan
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and GenymotionMichael Pan
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Michael Pan
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013Michael Pan
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by XcodeMichael Pan
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdfMichael Pan
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperMichael Pan
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. JackyMichael Pan
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017Michael Pan
 

Más de Michael Pan (13)

Shootting Game
Shootting GameShootting Game
Shootting Game
 
Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android Studio
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and Genymotion
 
Note something
Note somethingNote something
Note something
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by Xcode
 
Nimbus
NimbusNimbus
Nimbus
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdf
 
ADB - Arthur
ADB - ArthurADB - Arthur
ADB - Arthur
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game Developer
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. Jacky
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017
 

Homework2 play cards