热更新demo

TPSelectButton.m 6.6KB

    // // 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; - (id)initWithFrame:(CGRect)frame target:(id)target selector:(SEL)selector{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.topPadding = 0.1; /** * bug:6065(频道导航页:下方的8个icon有被切割的痕迹) */ 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); }]; if (frame.size.width != 0) { self.btnSize = frame.size; } } 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(TEXT_SIX_LEVELSIZE, 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:KEY_WINDOW]; 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, 20, 16)]; label.text = @"+1"; label.textColor = [UIColor colorWithHexString:BLUECOLOR]; label.font = appFont(16, NO); [UIView animateWithDuration:0.2 animations:^{ self.layer.transform = CATransform3DMakeScale(1.1, 1.1, 1.1); // self.transform = CGAffineTransformScale(self.transform, 1.1, 1.1); } completion:^(BOOL finished) { [UIView animateWithDuration:0.2 animations:^{ self.layer.transform = CATransform3DMakeScale(1.0/1.1, 1.0/1.1, 1.0/1.1); // self.transform = CGAffineTransformScale(self.transform, 1.0/1.1, 1.0/1.1); } completion:^(BOOL finished) { [KEY_WINDOW 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)layoutSubviews { CGSize imageSize = self.btnImage.size; if (imgSize.width != 0) { imageSize = self.imgSize; } if (self.isTopic == nil) { CGFloat textHeight = heightForString(self.textLabel.text, self.textLabel.font, CGRectGetWidth(self.bounds), NSLineBreakByWordWrapping); self.button.frame = self.bounds; [self.button setImageEdgeInsets:UIEdgeInsetsMake(self.topPadding, 0, CGRectGetHeight(self.bounds)-imageSize.height-self.topPadding, 0)]; /** * bug:6065(频道导航页:下方的8个icon有被切割的痕迹) */ // 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 { self.button.frame = self.bounds; [self.button setImageEdgeInsets:UIEdgeInsetsMake(self.topPadding, 0, CGRectGetHeight(self.bounds)-imageSize.height-self.topPadding, 0)]; self.textLabel.frame = CGRectMake(0, CGRectGetMaxY(self.frame)-15, CGRectGetWidth(self.bounds), 12); } /** * bug:5009(【适配性】iPhone4s点击话题闪退) */ self.imgInset = UIEdgeInsetsMake(1, self.btnSize.width/2-imageSize.width/2-1, self.btnSize.height-imageSize.height-self.topPadding+1, self.btnSize.width/2-imageSize.width/2-1); [super layoutSubviews]; } @end