仿苹果Assistive Touch

简单的实现苹果Assistive Touch。

自定义按钮:

//
//  YFAssistiveButton.m
//  仿 Assistive Touch
//
//  Created by dev on 16/6/21.
//  Copyright © 2016年 SWALLE. All rights reserved.
//

#import "YFAssistiveButton.h"
@interface YFAssistiveButton()
{
    CGPoint _beginPoint;
}
@end
@implementation YFAssistiveButton
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    
    _beginPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touvh = [touches anyObject];
    CGPoint currentPoint = [touvh locationInView:self];
    
    CGFloat X = currentPoint.x - _beginPoint.x;
    CGFloat Y = currentPoint.y - _beginPoint.y;
    self.center = CGPointMake(self.center.x + X, self.center.y + Y);
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGFloat W = self.frame.size.width/2 ;
    CGFloat H = self.frame.size.height/2;
    CGFloat KScreenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat KScreenH = [UIScreen mainScreen].bounds.size.height;
    
    CGFloat margenTop = self.center.y;
    CGFloat margenBottom = KScreenH - self.center.y;
    [UIView animateWithDuration:.2 animations:^{
        if (margenTop < 160) {
            self.center = CGPointMake(self.center.x + W, H);
            return;
        }else if (margenBottom < 160){
            self.center = CGPointMake(self.center.x + W, KScreenH - H);
            return;
        }else{
            if (self.center.x < KScreenW/2) {
                self.center = CGPointMake(W, self.center.y);
                return;
            }else if (self.center.x > KScreenW/2){
                self.center = CGPointMake(KScreenW - W, self.center.y);
                return;
            }
        }

    }];
    
    
    
}
@end

在控制器里面初始化自定义按钮

self.view.backgroundColor = [UIColor grayColor];
    YFAssistiveButton *btn = [YFAssistiveButton buttonWithType:UIButtonTypeSystem];
    _btn = btn;
    btn.layer.cornerRadius = 5;
    btn.frame = CGRectMake(5, 100, 50, 50);
    [btn setBackgroundImage:[UIImage imageNamed:@"Assistive Touch"] forState:UIControlStateNormal];
    
    [self.view addSubview:btn];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值