// // TPSelectButton.m // ThePaperHD // // Created by scar1900 on 15/2/12. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "TPSelectButton.h" @interface UIButton(selectButton) @end @implementation UIButton(selectButton) - (void)setBtnSelect:(BOOL)select animation:(BOOL)animation { if (self.selected == select) { return; } if (animation) { self.transform = CGAffineTransformMakeScale(1, 1); [UIView animateWithDuration:0.15 animations:^{ self.transform = CGAffineTransformMakeScale(0.9, 0.9); } completion:^(BOOL finished) { [UIView animateWithDuration:0.15 animations:^{ self.transform = CGAffineTransformMakeScale(1.1, 1.1); } completion:^(BOOL finished) { self.transform = CGAffineTransformMakeScale(1.0, 1.0); }]; }]; } self.selected = select; } @end @interface TPSelectButton() @property(nonatomic, strong)UIImage *btnImage; @end @implementation TPSelectButton @synthesize button,btnImage; @synthesize titleText = _titleText; @synthesize topPadding; @synthesize imgSize; @synthesize isPrasie = _isPrasie; - (id)initWithFrame:(CGRect)frame target:(id)target selector:(SEL)selector{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.topPadding = 0; self.button = [UIButton buttonWithType:UIButtonTypeCustom]; [self.button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.button]; [self addSubview:self.textLabel]; [self.textLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.left); make.right.equalTo(self.right); make.bottom.equalTo(self.bottom); make.height.equalTo(@12); }]; } return self; } - (void)setBUttonNormalImage:(UIImage*)normalImage highLightImage:(UIImage*)highLightImage selectedImage:(UIImage*)selectedImage { [self.button setImage:normalImage forState:UIControlStateNormal]; self.btnImage = normalImage; if (highLightImage) { [self.button setImage:highLightImage forState:UIControlStateHighlighted]; } if (selectedImage) { [self.button setImage:selectedImage forState:UIControlStateSelected]; } } - (UILabel*)textLabel { if (!_textLabel) { _textLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _textLabel.textColor = [UIColor colorWithHexString:TextGray]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.backgroundColor = [UIColor clearColor]; } _textLabel.font = appFont(11, NO); return _textLabel; } - (void)setTitleText:(NSString *)text { _titleText = text; self.textLabel.text = text; [self layoutSubviews]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { if (self.button.selected) { return; } [self.button setSelected:YES]; if (animated) { CGRect nowRect = [self convertRect:self.bounds toView:self.superview.superview]; CGFloat x = nowRect.origin.x; CGFloat y = nowRect.origin.y; CGFloat w = nowRect.size.width; CGFloat h = nowRect.size.height; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x+w/2-6, y+h/4-20, 40, 20)]; label.text = @"+1"; label.textColor = [UIColor colorWithHexString:BLUECOLOR]; label.font = appFont(16, NO); [UIView animateWithDuration:0.2 animations:^{ self.transform = CGAffineTransformScale(self.transform, 1.1, 1.1); } completion:^(BOOL finished) { [UIView animateWithDuration:0.2 animations:^{ self.transform = CGAffineTransformScale(self.transform, 1.0/1.1, 1.0/1.1); } completion:^(BOOL finished) { [self.superview.superview addSubview:label]; CGRect rect = label.frame; rect.origin.y -= 30; [UIView animateWithDuration:0.5 animations:^{ label.frame = rect; label.alpha = 0; } completion:^(BOOL finished) { [label removeFromSuperview]; }]; }]; }]; } } - (void)setSelect:(BOOL)select animation:(BOOL)animation{ [self.button setBtnSelect:select animation:animation]; } -(void)setIsPrasie:(BOOL)isPra{ _isPrasie = isPra; if (_isPrasie) { self.textLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; self.userInteractionEnabled = NO; } } - (void)layoutSubviews { if (self.isTopic == nil) { CGSize imageSize = self.btnImage.size; if (imgSize.width != 0) { imageSize = self.imgSize; } CGFloat textHeight = heightForString(self.textLabel.text, self.textLabel.font, CGRectGetWidth(self.bounds), NSLineBreakByWordWrapping); self.button.frame = self.bounds; [self.button setImageEdgeInsets:UIEdgeInsetsMake(self.topPadding, CGRectGetWidth(self.bounds)/2-imageSize.width/2, CGRectGetHeight(self.bounds)-imageSize.height-self.topPadding, CGRectGetWidth(self.bounds)/2-imageSize.width/2)]; // self.textLabel.frame = CGRectMake(0, CGRectGetMaxY(self.frame)-15, CGRectGetWidth(self.bounds), 12); self.textLabel.frame = CGRectMake(0, CGRectGetHeight(self.bounds)-textHeight, CGRectGetWidth(self.bounds), textHeight); }else { CGSize imageSize = self.btnImage.size; if (imgSize.width != 0) { imageSize = self.imgSize; } self.button.frame = self.bounds; [self.button setImageEdgeInsets:UIEdgeInsetsMake(self.topPadding, CGRectGetWidth(self.bounds)/2-imageSize.width/2, CGRectGetHeight(self.bounds)-imageSize.height-self.topPadding, CGRectGetWidth(self.bounds)/2-imageSize.width/2)]; self.textLabel.frame = CGRectMake(0, CGRectGetMaxY(self.frame)-15, CGRectGetWidth(self.bounds), 12); } /** * bug:5009(【适配性】iPhone4s点击话题闪退) */ [super layoutSubviews]; } @end