Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. @interface HWQuestionControl : UIControl{
  2.  
  3. }
  4. @property(nonatomic, strong)UILabel *titleLabel;
  5. @property(nonatomic, strong)UIImageView *imageView;
  6.  
  7. @end
  8.  
  9. @interface HWQuestionControl (){
  10.  
  11. }
  12. @property(nonatomic, strong)UIView *fillView;
  13.  
  14. @end
  15.  
  16. @implementation HWQuestionControl
  17.  
  18. -(id)initWithCoder:(NSCoder *)aDecoder{
  19. self = [super initWithCoder:aDecoder];
  20. if(self){
  21. [self initUI];
  22. }
  23. return self;
  24. }
  25.  
  26. -(instancetype)initWithFrame:(CGRect)frame{
  27. self = [super initWithFrame:frame];
  28. if(self){
  29. [self initUI];
  30. }
  31. return self;
  32. }
  33.  
  34. #pragma mark - UI
  35.  
  36. -(void)initUI{
  37. UIColor *highlightColor = [AppStyle buttonHighlightColor];
  38.  
  39. self.layer.cornerRadius = (self.frame.size.height / 2);
  40. self.clipsToBounds = YES;
  41. [self setBackgroundColor:[UIColor whiteColor]];
  42. self.titleLabel.textAlignment = NSTextAlignmentLeft;
  43.  
  44. //Border
  45. self.layer.borderWidth = 0;
  46. self.layer.borderColor = highlightColor.CGColor;
  47.  
  48. //Pressed state
  49. [self addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  50. [self addTarget:self action:@selector(wasPressed:) forControlEvents:UIControlEventTouchUpInside];
  51. [self addTarget:self action:@selector(wasPressed:) forControlEvents:UIControlEventTouchDown];
  52.  
  53. //Unpressed state
  54. [self addTarget:self action:@selector(wasUnpressed:) forControlEvents:UIControlEventTouchDragExit];
  55. [self addTarget:self action:@selector(wasUnpressed:) forControlEvents:UIControlEventTouchCancel];
  56.  
  57. //Label
  58. self.titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
  59. self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  60. self.titleLabel.textColor = highlightColor;
  61. self.titleLabel.text = @"A difference maker";
  62. self.titleLabel.textAlignment = NSTextAlignmentLeft;
  63. [self addSubview:self.titleLabel];
  64.  
  65. //Image
  66. self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smiley"]];
  67. self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
  68. [self addSubview:self.imageView];
  69.  
  70. //Fill view
  71. self.fillView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, self.frame.size.height)];
  72. self.fillView.translatesAutoresizingMaskIntoConstraints = NO;
  73. self.fillView.backgroundColor = [AppStyle buttonFillColor];
  74. self.fillView.userInteractionEnabled = NO;
  75. [self addSubview:self.fillView];
  76. [self sendSubviewToBack:self.fillView];
  77.  
  78. //Layout
  79. [self setUpAutoLayout];
  80. }
  81.  
  82. #pragma mark - Autolayout
  83.  
  84. -(void)setUpAutoLayout{
  85. //Verbose autolayout. A library or categories should help with this but for simplicity I kept it...
  86. NSDictionary *viewsDictionary = @{@"fillView":self.fillView,
  87. @"imageView":self.imageView,
  88. @"titleLabel":self.titleLabel,
  89. @"hSpacing": @(20)};
  90.  
  91. //Image
  92. NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView(30)]"
  93. options:0
  94. metrics:nil
  95. views:viewsDictionary];
  96. NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageView(30)]"
  97. options:0
  98. metrics:nil
  99. views:viewsDictionary];
  100. [self.imageView addConstraints:constraint_H];
  101. [self.imageView addConstraints:constraint_V];
  102.  
  103. NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:self.imageView
  104. attribute:NSLayoutAttributeCenterY
  105. relatedBy:NSLayoutRelationEqual
  106. toItem:self
  107. attribute:NSLayoutAttributeCenterY
  108. multiplier:1.0
  109. constant:0.0];
  110. [self addConstraint:centerYConstraint];
  111.  
  112. NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[imageView]"
  113. options:0
  114. metrics:nil
  115. views:viewsDictionary];
  116. [self addConstraints:constraint_POS_H];
  117.  
  118.  
  119. //Label
  120. constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleLabel(30)]"
  121. options:0
  122. metrics:nil
  123. views:viewsDictionary];
  124. [self.titleLabel addConstraints:constraint_V];
  125.  
  126. constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[imageView]-15-[titleLabel]"
  127. options:0
  128. metrics:nil
  129. views:viewsDictionary];
  130. [self addConstraints:constraint_POS_H];
  131.  
  132. centerYConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel
  133. attribute:NSLayoutAttributeCenterY
  134. relatedBy:NSLayoutRelationEqual
  135. toItem:self
  136. attribute:NSLayoutAttributeCenterY
  137. multiplier:1.0
  138. constant:0.0];
  139. [self addConstraint:centerYConstraint];
  140. }
  141.  
  142. #pragma mark - Actions
  143.  
  144. -(void)touchUpInside:(id)sender{
  145. [self animateFillBarWithPercentage:0.25f];
  146. }
  147.  
  148. -(void)wasPressed:(id)sender{
  149. self.layer.borderWidth = 2;
  150. }
  151.  
  152. -(void)wasUnpressed:(id)sender{
  153. self.layer.borderWidth = 0;
  154. }
  155.  
  156. #pragma mark - Animation
  157.  
  158. -(void)animateFillBarWithPercentage:(CGFloat)fillPercenatage{
  159. __weak typeof(self)weakSelf = self;
  160.  
  161. CGFloat fillWidth = self.frame.size.width * fillPercenatage;
  162. [UIView animateWithDuration:0.25 delay:0.05 options:UIViewAnimationOptionAllowUserInteraction animations:^{
  163. weakSelf.fillView.frame = CGRectMake(0, 0, fillWidth, weakSelf.fillView.frame.size.height);
  164. } completion:^(BOOL finished) {
  165. NSLog(@"Hurray");
  166. }];
  167. }
  168.  
  169. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement