Skip to content

Commit ff11140

Browse files
committed
minor refactoring
1 parent 14eb500 commit ff11140

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

TableviewFlip/ViewController.m

+29-32
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111

1212
#define kFlipVerticalTime 0.5
1313
#define kFlipHorizontalTime 0.6
14+
#define kDelayTime 0.0
15+
#define kAnimationEasingType UIViewAnimationOptionCurveEaseInOut
1416

1517
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
1618
@property IBOutlet UITableView *tableView;
1719
@property NSArray *tableViewDataSource;
1820
@end
1921

20-
typedef enum : NSUInteger {
21-
kClockWiseDirection = -1,
22-
kAntiClockWiseDirection = 1,
23-
} kRotationDirection;
2422

2523
@implementation ViewController
2624

@@ -80,12 +78,12 @@ - (void)buttonPressed:(UIButton *)sender {
8078
NSTimeInterval timeInterval = kFlipHorizontalTime;
8179
NSString *msg = @"Hello is now changed";
8280
int tag = 3;
83-
cell.layer.transform = [self performHorizontalFlipInDirection:kClockWiseDirection];
81+
cell.layer.transform = [self performHorizontalFlipInClockwiseDirection:YES];
8482
if (sender.tag) {
8583
tag = 4;
8684
timeInterval = kFlipVerticalTime;
8785
msg = @"World is now changed";
88-
cell.layer.transform = [self performVerticalFlipInDirection:kClockWiseDirection];
86+
cell.layer.transform = [self performVerticalFlipInClockwiseDirection:YES];
8987
}
9088

9189

@@ -100,8 +98,8 @@ - (void)buttonPressed:(UIButton *)sender {
10098
NSString *imageName = [NSString stringWithFormat:@"%d.jpg", tag];
10199

102100
[UIView animateWithDuration:timeInterval
103-
delay:0.0
104-
options:UIViewAnimationOptionCurveEaseInOut
101+
delay:kDelayTime
102+
options:kAnimationEasingType
105103
animations: ^{
106104
cell.layer.transform = CATransform3DIdentity;
107105
cell.textLabel.textColor = [UIColor whiteColor];
@@ -122,11 +120,12 @@ - (void)revert:(UIButton *)sender {
122120

123121
NSString *msg = @"Hello";
124122
NSTimeInterval timeInterval = kFlipHorizontalTime;
125-
cell.layer.transform = [self performHorizontalFlipInDirection:kClockWiseDirection];
123+
cell.layer.transform = [self performHorizontalFlipInClockwiseDirection:YES];
126124
if (sender.tag) {
127125
timeInterval = kFlipVerticalTime;
128126
msg = @"World";
129-
cell.layer.transform = [self performVerticalFlipInDirection:kClockWiseDirection];
127+
cell.layer.transform = [self performVerticalFlipInClockwiseDirection:YES];
128+
130129
}
131130

132131
NSString *imageName = [NSString stringWithFormat:@"%d.jpg", sender.tag];
@@ -141,8 +140,8 @@ - (void)revert:(UIButton *)sender {
141140
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
142141

143142
[UIView animateWithDuration:timeInterval
144-
delay:0.0
145-
options:UIViewAnimationOptionCurveEaseInOut
143+
delay:kDelayTime
144+
options:kAnimationEasingType
146145
animations: ^{
147146
cell.layer.transform = CATransform3DIdentity;
148147
cell.textLabel.textColor = [UIColor whiteColor];
@@ -158,36 +157,34 @@ - (void)revert:(UIButton *)sender {
158157
}];
159158
}
160159

161-
- (CATransform3D)performVerticalFlipInDirection:(kRotationDirection)clockwise {
162-
return [self getTransformForIndex:1 inDirection:kClockWiseDirection];
160+
- (CATransform3D)performVerticalFlipInClockwiseDirection:(BOOL)clockwise {
161+
return [self getTransformForIndex:1 inClockwiseDirection:clockwise];
163162
}
164163

165-
- (CATransform3D)performHorizontalFlipInDirection:(kRotationDirection)clockwise {
166-
return [self getTransformForIndex:0 inDirection:clockwise];
164+
- (CATransform3D)performHorizontalFlipInClockwiseDirection:(BOOL)clockwise {
165+
return [self getTransformForIndex:0 inClockwiseDirection:clockwise];
167166
}
168167

169-
- (CATransform3D)getTransformForIndex:(int)index inDirection:(kRotationDirection)clockwise {
170-
int direction = -1;
168+
- (CATransform3D)getTransformForIndex:(int)index inClockwiseDirection:(BOOL)clockwise {
169+
170+
int direction = clockwise ? -1 : 1;
171171

172-
float xCord = 0.0;
173-
float yCord = 1.0;
172+
float xCord = index ? 1.0 : 0.0;
173+
float yCord = index ? 0.0 : 1.0;
174174
float zCord = 0.0;
175175

176+
//float angle = (180 * direction) * (M_PI / 180);
177+
float angle = direction * M_PI;
176178

177-
if (clockwise) direction = 1;
179+
CGPoint offset = CGPointMake(0, 0);
178180

179-
float rotateAngleBy = (180 * direction) * (M_PI / 180);
180-
CGPoint offsetPositioning = CGPointMake(0, 0);
181+
CATransform3D transform = CATransform3DIdentity;
182+
183+
transform = CATransform3DRotate(transform, angle, xCord, yCord, zCord);
184+
185+
transform = CATransform3DTranslate(transform, offset.x, offset.y, 0.0);
181186

182-
CATransform3D transform = CATransform3DIdentity;
183-
184-
if (index) {
185-
xCord = 1.0; yCord = 0.0;
186-
}
187-
188-
transform = CATransform3DRotate(transform, rotateAngleBy, xCord, yCord, zCord);
189-
190-
transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, 0.0);
187+
191188
return transform;
192189
}
193190

0 commit comments

Comments
 (0)