美文网首页iOS相关
iOS自定义按钮的水波效果加震动

iOS自定义按钮的水波效果加震动

作者: 薰衣草儿 | 来源:发表于2018-05-28 10:02 被阅读152次

很多按钮点击了没有明显的效果,于是自定义个带有水波效果的button 代码如下 .m文件


#import "HOWaveButton.h"

@interface HOButtonCircleSet : NSObject

@property CGFloat circleCenterX;

@property CGFloat circleCenterY;

@property CGFloat circleWidth;

@property CGFloat circleRait;

@end

@implementation HOButtonCircleSet

@end

@interface HOWaveButton ()

@property (nonatomic, assign) NSInteger loopCount;

@property (nonatomic, strong) NSMutableDictionary *circles;

@property (nonatomic, assign) NSInteger circleFalg;

@property (nonatomic, assign) NSTimeInterval AnimationDuration;

@end

@implementation HOWaveButton

- (instancetype)initWithFrame:(CGRect)frame BackColor:(UIColor *)backColor HaveRadius:(BOOL)haveRadius Radius:(CGFloat)radius{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = backColor;

        if (haveRadius) {

            self.layer.cornerRadius = radius;

            self.clipsToBounds = YES;

        }

        self.loopCount = 1 / 0.02;

        self.circles = [NSMutableDictionary dictionary];

        self.circleFalg = 0;

        [self addTarget:self action:@selector(touchedDown:event:) forControlEvents:UIControlEventTouchUpInside];

    }

    return self;

}

- (void)touchedDown:(HOWaveButton *)btn event:(UIEvent *)event{

    UITouch *touch = event.allTouches.allObjects.firstObject;

    CGPoint touchPoint = [touch locationInView:btn];

//    CGPoint touchPoint = CGPointMake(self.width *0.5, self.height * 0.5);

    NSString *key = [NSString stringWithFormat:@"%ld",self.circleFalg];

    HOButtonCircleSet *set = [HOButtonCircleSet new];

    set.circleCenterX = touchPoint.x;

    set.circleCenterY = touchPoint.y;

    set.circleRait = 0;

    CGFloat maxX = touchPoint.x > (self.width - touchPoint.x) ? touchPoint.x : (self.width - touchPoint.x);

    CGFloat maxY = touchPoint.y > (self.width - touchPoint.y) ? touchPoint.y : (self.width - touchPoint.y);

    set.circleWidth = maxX > maxY ? maxX :maxY;

    [self.circles setObject:set forKey:key];

    NSTimer *timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(timeFunction:) userInfo:@{@"key":key} repeats:YES];

    [NSRunLoop.mainRunLoop addTimer:timer forMode:NSRunLoopCommonModes];

    self.circleFalg ++;

}

- (void)timeFunction:(NSTimer *)timer{

    [self setNeedsDisplay];

    NSDictionary *userInfo = timer.userInfo;

    NSString *key = userInfo[@"key"];

    HOButtonCircleSet *set = self.circles[key];

    if (set.circleRait <= 1) {

        set.circleRait += (1.0/self.loopCount);

    }else{

        [self.circles removeObjectForKey:key];

        [timer invalidate];

    }

}

- (void)drawRect:(CGRect)rect{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGFloat endAngle = M_PI * 2;

    for (HOButtonCircleSet *circleSet in self.circles.allValues) {

        CGContextAddArc(context, circleSet.circleCenterX, circleSet.circleCenterY, circleSet.circleWidth * circleSet.circleRait, 0, endAngle, NO);

        [[[UIColor whiteColor] colorWithAlphaComponent:(1-circleSet.circleRait)] setStroke];

        [[[UIColor whiteColor] colorWithAlphaComponent:(1-circleSet.circleRait)] setFill];

        CGContextFillPath(context);

    }

}

.h文件


@interface HOWaveButton : UIButton

- (instancetype)initWithFrame:(CGRect)frame BackColor:(UIColor *)backColor HaveRadius:(BOOL)haveRadius Radius:(CGFloat)radius;

@end

效果如下

image

还可以在点击的时候加上震动效果,不过震动效果是必须在硬件iPhone7或者iPhone7以上才有的 两行代搞定


if (@available(iOS 10.0, *)) {

                UIImpactFeedbackGenerator *impactor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy];

                [impactor impactOccurred];

            }

完美搞定

相关文章

网友评论

本文标题:iOS自定义按钮的水波效果加震动

本文链接:https://www.haomeiwen.com/subject/yqpwjftx.html