iOS自定義按鈕的水波效果加震動

很多按鈕點擊了沒有明顯的效果,于是自定義個帶有水波效果的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];

            }

完美搞定

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。