| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- //
- // PopMenuButton.m
- // HyPopMenuView
- //
- // Created by Hy_Mac on 16/7/8.
- // Copyright © 2016年 ouy.Aberi. All rights reserved.
- //
- #import "PopMenuButton.h"
- #import "UIColor+ImageGetColor.h"
- @implementation PopMenuButton
- static NSString* animationKey = @"transform.scale";
- + (instancetype __nonnull)buttonWithType:(UIButtonType)buttonType
- {
- PopMenuButton* button = [super buttonWithType:buttonType];
- return button;
- }
- - (instancetype __nonnull)init
- {
- self = [super initWithFrame:CGRectNull];
- if (self) {
- self.titleLabel.alpha = 0.0f;
- //1.文字居中
- self.titleLabel.textAlignment = NSTextAlignmentCenter;
- //2.文字大小
- self.titleLabel.font = [UIFont systemFontOfSize:13];
- //3.图片的内容模式
- self.imageView.contentMode = UIViewContentModeScaleAspectFit;
- self.imageView.clipsToBounds = true;
- //4.取消高亮
- self.adjustsImageWhenHighlighted = false;
- [self addTarget:self action:@selector(scaleToSmall)
- forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
- [self addTarget:self action:@selector(scaleToDefault)
- forControlEvents:UIControlEventTouchDragExit];
- self.layer.masksToBounds = true;
- }
- return self;
- }
- - (void)setTextLabelWithShadowColor:(UIColor*)color
- {
- UILabel* textLabel = [self viewWithTag:21];
- if (!textLabel) {
- textLabel = [UILabel new];
- textLabel.tag = 21;
- textLabel.textAlignment = NSTextAlignmentCenter;
- textLabel.font = [UIFont systemFontOfSize:13];
- [self addSubview:textLabel];
- }
- [textLabel setFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 20, CGRectGetWidth(self.bounds), 20)];
- [textLabel setTextColor:color];
- [textLabel setText:_model.titleString];
- textLabel.shadowColor = [UIColor whiteColor];
- textLabel.shadowOffset = CGSizeMake(0.3f, 0.3f);
- }
- - (void)scaleToSmall
- {
- CABasicAnimation* theAnimation;
- theAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
- theAnimation.delegate = self;
- theAnimation.duration = 0.1;
- theAnimation.repeatCount = 0;
- theAnimation.removedOnCompletion = FALSE;
- theAnimation.fillMode = kCAFillModeForwards;
- theAnimation.autoreverses = NO;
- theAnimation.fromValue = [NSNumber numberWithFloat:1];
- theAnimation.toValue = [NSNumber numberWithFloat:1.2f];
- [self.imageView.layer addAnimation:theAnimation forKey:theAnimation.keyPath];
- }
- - (void)scaleToDefault
- {
- CABasicAnimation* theAnimation;
- theAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
- theAnimation.delegate = self;
- theAnimation.duration = 0.1;
- theAnimation.repeatCount = 0;
- theAnimation.removedOnCompletion = FALSE;
- theAnimation.fillMode = kCAFillModeForwards;
- theAnimation.autoreverses = NO;
- theAnimation.fromValue = [NSNumber numberWithFloat:1.2f];
- theAnimation.toValue = [NSNumber numberWithFloat:1];
- [self.imageView.layer addAnimation:theAnimation forKey:theAnimation.keyPath];
- }
- - (void)selectdAnimation
- {
- if (_model.transitionType == PopMenuTransitionTypeSystemApi) {
- CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
- scaleAnimation.delegate = self;
- scaleAnimation.duration = 0.2;
- scaleAnimation.repeatCount = 0;
- scaleAnimation.removedOnCompletion = FALSE;
- scaleAnimation.fillMode = kCAFillModeForwards;
- scaleAnimation.autoreverses = NO;
- scaleAnimation.fromValue = @1;
- scaleAnimation.toValue = @1.4;
- CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- opacityAnimation.delegate = self;
- opacityAnimation.duration = 0.2;
- opacityAnimation.repeatCount = 0;
- opacityAnimation.removedOnCompletion = FALSE;
- opacityAnimation.fillMode = kCAFillModeForwards;
- opacityAnimation.autoreverses = NO;
- opacityAnimation.fromValue = @1;
- opacityAnimation.toValue = @0;
- [self.layer addAnimation:scaleAnimation forKey:scaleAnimation.keyPath];
- [self.layer addAnimation:opacityAnimation forKey:opacityAnimation.keyPath];
- }
- else {
- self.userInteractionEnabled = false;
- self.layer.cornerRadius = CGRectGetWidth(self.bounds) / 2;
- NSLog(@"%.2f",CGRectGetWidth(self.bounds) / 2);
- UIImage* image = self.imageView.image;
- UIColor* color = [UIColor getPixelColorAtLocation:CGPointMake(50, 20) inImage:image];
- [self setBackgroundColor:color];
- if (_model.transitionRenderingColor) {
- [self setBackgroundColor:_model.transitionRenderingColor];
- }
- UILabel* textLabel = [self viewWithTag:21];
- textLabel.text = @"";
- [self setTitle:@"" forState:UIControlStateNormal];
- [self setImage:nil forState:UIControlStateNormal];
- CABasicAnimation* expandAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- expandAnim.fromValue = @(1.0);
- expandAnim.toValue = @(33.0);
- expandAnim.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.95:0.02:1:0.05];
- expandAnim.duration = 0.3;
- expandAnim.delegate = self;
- expandAnim.fillMode = kCAFillModeForwards;
- expandAnim.removedOnCompletion = false;
- expandAnim.autoreverses = NO;
- [self.layer addAnimation:expandAnim forKey:expandAnim.keyPath];
- }
- }
- - (void)cancelAnimation
- {
- CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
- scaleAnimation.delegate = self;
- scaleAnimation.duration = 0.3;
- scaleAnimation.repeatCount = 0;
- scaleAnimation.removedOnCompletion = FALSE;
- scaleAnimation.fillMode = kCAFillModeForwards;
- scaleAnimation.autoreverses = NO;
- scaleAnimation.fromValue = @1;
- scaleAnimation.toValue = @0.3;
- CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- opacityAnimation.delegate = self;
- opacityAnimation.duration = 0.3;
- opacityAnimation.beginTime = 0;
- opacityAnimation.repeatCount = 0;
- opacityAnimation.removedOnCompletion = false;
- opacityAnimation.fillMode = kCAFillModeForwards;
- opacityAnimation.autoreverses = NO;
- opacityAnimation.fromValue = @1;
- opacityAnimation.toValue = @0;
- //CGAffineTransformIdentity
- [self.layer addAnimation:scaleAnimation forKey:[NSString stringWithFormat:@"cancel%@", scaleAnimation.keyPath]];
- [self.layer addAnimation:opacityAnimation forKey:opacityAnimation.keyPath];
- }
- - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
- {
- CABasicAnimation* cab = (CABasicAnimation*)anim;
- if ([cab.toValue floatValue] == 33.0f || [cab.toValue floatValue] == 1.4f) {
- [self setUserInteractionEnabled:true];
- __weak PopMenuButton* weakButton = self;
- if (weakButton.block) {
- weakButton.block(self);
- }
- [NSTimer scheduledTimerWithTimeInterval:0.6f target:self selector:@selector(DidStopAnimation) userInfo:nil repeats:nil];
- }
- }
- - (void)DidStopAnimation
- {
- [self.layer removeAllAnimations];
- }
- #pragma mark 调整内部ImageView的frame
- - (CGRect)imageRectForContentRect:(CGRect)contentRect
- {
- CGFloat imageWidth = contentRect.size.width / 2.65;
- CGFloat imageX = CGRectGetWidth(contentRect) / 2 - imageWidth / 2;
- CGFloat imageHeight = imageWidth;
- CGFloat imageY = CGRectGetHeight(self.bounds) - (imageHeight + 30);
- return CGRectMake(imageX, imageY, imageWidth, imageHeight);
- }
- #pragma mark 调整内部UILabel的frame
- - (CGRect)titleRectForContentRect:(CGRect)contentRect
- {
- CGFloat titleX = 0;
- CGFloat titleHeight = 20;
- CGFloat titleY = contentRect.size.height - titleHeight;
- CGFloat titleWidth = contentRect.size.width;
- return CGRectMake(titleX, titleY, titleWidth, titleHeight);
- }
- - (void)setModel:(PopMenuModel*)model
- {
- _model = model;
- UIImage* image = [UIImage imageNamed:model.imageNameString];
- [self setImage:image forState:UIControlStateNormal];
- [self setTitle:model.titleString forState:UIControlStateNormal];
- UIColor* tempColor = nil;
- UIColor* color = [UIColor getPixelColorAtLocation:CGPointMake(image.size.width / 2, image.size.height / 2) inImage:image];
- // [self setTitleColor:color forState:UIControlStateNormal];
- tempColor = color;
- if (!_model.automaticIdentificationColor) {
- if (_model.textColor) {
- //[self setTitleColor:model.textColor forState:UIControlStateNormal];
- tempColor = model.textColor;
- }
- }
- [self setTextLabelWithShadowColor:tempColor];
- }
- @end
|