-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComposeAnswerCell.m
91 lines (75 loc) · 2.45 KB
/
ComposeAnswerCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// ComposeAnswerCell.m
// unnamed
//
// Created by Bruce Ng on 2/18/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//
#import "ComposeAnswerCell.h"
@interface ComposeAnswerCell()
@property (weak, nonatomic) IBOutlet UITextField *answerText;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (nonatomic, strong) NSArray *valueLabels;
@end
@implementation ComposeAnswerCell
- (void)awakeFromNib {
// Initialization code
self.valueLabels = @[@"1.", @"2.", @"3.", @"4.", @"5.", @"6."];
self.answerLabel.alpha = 0;
self.answerText.alpha = 1;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
tapGesture.delegate = self;
self.answerLabel.userInteractionEnabled = YES;
[self.answerLabel addGestureRecognizer:tapGesture];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
}
- (void)setAnswer:(Answer *)answer {
_answer = answer;
self.answerLabel.text = answer.text;
self.answerText.text = answer.text;
if ([answer.text length] > 0) {
self.answerLabel.alpha = 1;
self.answerText.alpha = 0;
} else {
self.answerLabel.alpha = 0;
self.answerText.alpha = 1;
}
}
- (void)setAnswerIndex:(NSInteger)answerIndex {
_answerIndex = answerIndex;
self.valueLabel.text = self.valueLabels[answerIndex];
}
#pragma mark - Gesture Recognizers
- (IBAction)onTap:(UITapGestureRecognizer *)sender {
[self showTextField:YES];
}
- (IBAction)onEditingEnd:(id)sender {
if ([self.answerText.text length] > 0) {
[self showTextField:NO];
} else {
[self showTextField:YES];
}
[self.delegate composeAnswerCell:self changedAnswer:self.answer];
}
- (IBAction)onEditingChanged:(id)sender {
self.answerLabel.text = self.answerText.text;
self.answer.text = self.answerText.text;
[self.delegate composeAnswerCell:self changedAnswer:self.answer];
}
#pragma mark - Private methods
- (void)showTextField:(BOOL)show {
[UIView animateWithDuration:0.2 animations:^{
if (show) {
self.answerLabel.alpha = 0;
self.answerText.text = self.answerLabel.text;
self.answerText.alpha = 1;
} else {
self.answerLabel.alpha = 1;
self.answerLabel.text = self.answerText.text;
self.answerText.alpha = 0;
}
}];
}
@end