BaseControl按钮合集_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > BaseControl按钮合集

BaseControl按钮合集

 2016/5/27 0:33:03  YouXianMing  程序员俱乐部  我要评论(0)
  • 摘要:BaseControl按钮合集效果源码https://github.com/YouXianMing/Animations////POPBaseControl.h//Animations////CreatedbyYouXianMingon16/5/26.//Copyright©2016年YouXianMing.Allrightsreserved.//#import<UIKit/UIKit.h>@classPOPBaseControl
  • 标签:ASE

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种按钮的基类控件,以上是一个示例演示,演示如何通过继承来实现想要的效果.

 

发表评论
用户名: 匿名