BaseControl按钮合集

BaseControl按钮合集

 

效果

 

源码

https://github.com/YouXianMing/Animations

//
//  POPBaseControl.h
//  Animations
//
//  Created by YouXianMing on 16/5/26.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
@class POPBaseControl;

@protocol POPBaseControlDelegate <NSObject>

/**
 *  缩放百分比事件
 *
 *  @param controll PressControll对象
 *  @param percent  百分比
 */
- (void)POPBaseControl:(POPBaseControl *)controll currentPercent:(CGFloat)percent;

/**
 *  事件触发
 *
 *  @param controll PressControll对象
 */
- (void)POPBaseControlEvent:(POPBaseControl *)controll;

@end

@interface POPBaseControl : UIView

/**
 *  代理
 */
@property (nonatomic, weak) id <POPBaseControlDelegate>  delegate;

/**
 *  动画时间,默认值为0.4
 */
@property (nonatomic) CFTimeInterval animationDuration;

/**
 *  目标对象
 */
@property (nonatomic, weak) id target;

/**
 *  事件
 */
@property (nonatomic) SEL      selector;

/**
 *  是否有效
 */
@property (nonatomic) BOOL     enabled;

/**
 *  是否选中
 */
@property (nonatomic) BOOL     selected;

#pragma mark - Properties used by SubClass & Methods Overwrite by subClass.

/**
 *  容器view,用于子类添加控件
 */
@property (nonatomic, strong, readonly) UIView  *contentView;

/**
 *  当前动画比例(子类继承的时候重载)
 *
 *  @param percent 比例
 */
- (void)currentPercent:(CGFloat)percent;

/**
 *  事件激活了
 */
- (void)controllEventActived;

@end
//
//  POPBaseControl.m
//  Animations
//
//  Created by YouXianMing on 16/5/26.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "POPBaseControl.h"
#import "POP.h"

@interface POPBaseControl ()

@property (nonatomic, strong) UIView   *absView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIView   *contentView;

@property (nonatomic) CGFloat percent;

@end

@implementation POPBaseControl

- (void)layoutSubviews {
    
    [super layoutSubviews];
    _button.frame       = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _contentView.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        // 动画时间
        _animationDuration = 0.4f;
        
        // 隐身的view
        _absView                        = [[UIView alloc] init];
        _absView.userInteractionEnabled = NO;
        _absView.backgroundColor        = [UIColor clearColor];
        [self addSubview:_absView];
        
        // 容器View
        _contentView                        = [[UIView alloc] initWithFrame:self.bounds];
        _contentView.userInteractionEnabled = NO;
        [self addSubview:_contentView];
        
        // 按钮
        _button = [[UIButton alloc] initWithFrame:self.bounds];
        [self addSubview:_button];
        
        // 按钮事件
        [_button addTarget:self action:@selector(touchBeginOrTouchDragEnter) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
        [_button addTarget:self action:@selector(touchUpInside)   forControlEvents:UIControlEventTouchUpInside];
        [_button addTarget:self action:@selector(touchDragExitOrTouchCancel)   forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];
    }
    
    return self;
}

#pragma mark - Animations.

- (void)touchUpInside {
    
    [self touchDragExitOrTouchCancel];
    
    [self controllEventActived];
    
    if (self.target && self.selector) {
        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.target performSelector:self.selector withObject:self];
#pragma clang diagnostic pop
        
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(POPBaseControlEvent:)]) {
        
        [self.delegate POPBaseControlEvent:self];
    }
}

- (void)touchDragExitOrTouchCancel {
    
    [_absView.layer pop_removeAllAnimations];
    
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
    scaleAnimation.toValue            = @(1);
    scaleAnimation.delegate           = self;
    scaleAnimation.duration           = _animationDuration;
    [_absView.layer pop_addAnimation:scaleAnimation forKey:nil];
}

- (void)touchBeginOrTouchDragEnter {
    
    [_absView.layer pop_removeAllAnimations];
    
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
    scaleAnimation.toValue            = @(0);
    scaleAnimation.delegate           = self;
    scaleAnimation.duration           = _animationDuration;
    [_absView.layer pop_addAnimation:scaleAnimation forKey:nil];
}

#pragma mark - POPAnimation's delegate.

- (void)pop_animationDidApply:(POPAnimation *)anim {
    
    NSNumber *toValue = (NSNumber *)[anim valueForKeyPath:@"currentValue"];
    _percent          = (toValue.floatValue - [POPBaseControl calculateConstantWithX1:0 y1:1 x2:1 y2:0]) / [POPBaseControl calculateSlopeWithX1:0 y1:1 x2:1 y2:0];
    
    [self currentPercent:_percent];
}

#pragma mark - Overwrite by subClass.

- (void)currentPercent:(CGFloat)percent {
    
}

- (void)controllEventActived {
    
}

#pragma mark - Math.

+ (CGFloat)calculateSlopeWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 {
    
    return (y2 - y1) / (x2 - x1);
}

+ (CGFloat)calculateConstantWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 {
    
    return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
}

#pragma mark - setter & getter.

@synthesize enabled = _enabled;

- (void)setEnabled:(BOOL)enabled {
    
    _button.enabled = enabled;
}

- (BOOL)enabled {
    
    return _button.enabled;
}

@synthesize selected = _selected;

- (void)setSelected:(BOOL)selected {
    
    _button.selected = selected;
}

- (BOOL)selected {
    
    return _button.selected;
}

@end

 

说明

本人一共封装了3种按钮的基类控件,以上是一个示例演示,演示如何通过继承来实现想要的效果.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值