iOS 模仿assistiveTouch按钮

项目在需求在界面上添加一个悬浮按钮,最近闲着没事做,正好在简书上看到一篇文章,我就参考一下,改成iOS系统的assistiveTouch.
效果参考下系统的assistiveTouch.按钮.

参考图片:


touchButton.gif

下面是代码!


#pragma mark -手势事件改变分享按钮位置
-(void)changePostion:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan translationInView:_shareButton];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGRect originalFrame = _shareButton.frame;
    if (originalFrame.origin.x >= 0 && originalFrame.origin.x+originalFrame.size.width <= width) {
        originalFrame.origin.x += point.x;
    }if (originalFrame.origin.y >= 0 && originalFrame.origin.y+originalFrame.size.height <= height) {
        originalFrame.origin.y += point.y;
    }
    _shareButton.frame = originalFrame;
    [pan setTranslation:CGPointZero inView:_shareButton];/**/
    if (pan.state == UIGestureRecognizerStateBegan) {
        _shareButton.enabled = NO;
    }else if (pan.state == UIGestureRecognizerStateChanged){
    } else {
        CGRect frame = _shareButton.frame;
        //是否越界
        BOOL isOver = NO;
        if (frame.origin.x < 0  ) {                                                           // 左边x
            frame.origin.x = 0;
            isOver = YES;
        } else if (frame.origin.x+frame.size.width > width ) {            //右边x
            frame.origin.x = width - frame.size.width;
            isOver = YES;
        }
        if (frame.origin.y < 64  ) {                                                            // 顶部y
            frame.origin.y = 0;
            isOver = YES;
        } else if (frame.origin.y + frame.size.height >= height - 64 ) { //底部y
            frame.origin.y = height - frame.size.height;
            isOver = YES;
            
        // 设置靠边生效范围 在距离顶部64到底部上来64
        }else if ( frame.origin.y > 64 & frame.origin.y < height - 64){
            if ( frame.origin.x + originalFrame.size.width / 2 < width/2 ) {
                frame.origin.x = 0;                                         //左边靠
            }else{
                frame.origin.x = width - frame.size.width;  //右边靠
            }
            isOver = YES;
        }
        if (isOver) {
            [UIView animateWithDuration:0.3 animations:^{
                _shareButton.frame = frame;
            }];
        }
        _shareButton.enabled = YES;
    }
}
#pragma mark -lazy
- (UIButton *)shareButton{
    
    if (_shareButton == nil) {
        _shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _shareButton.showsTouchWhenHighlighted = NO;
        _shareButton.backgroundColor  = [UIColor redColor];
        [_shareButton setBackgroundImage:[UIImage imageNamed:@"shareFriends"] forState:UIControlStateNormal];
        _shareButton.frame = CGRectMake(self.view.frame.size.width - 70 - 12/2, self.view.frame.size.height - 114/2 - 70 , 60, 60);
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];
        [_shareButton addGestureRecognizer:pan];
    }
    return _shareButton;
}

参考文章地址:http://www.jianshu.com/p/24999f21dbcf

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,262评论 25 709
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,268评论 8 183
  • 昨晚做了一个彩色的梦,很美好。但是经历过的美好也常常让自己感到怅然。都说要活在当下,然当下却常常是空荡。但一定要做...
    _种瓜得豆__阅读 1,313评论 0 0
  • 我是一只衔着柳笛的鸟,在树林中不断地叫着。在叫声中,青山醒了,大地醒了,世界在苏醒。 隔着树木,我看见迊春的花,在...
    倚剑白云天阅读 2,578评论 1 0
  • 今天是高考第一日,圈裡的朋友紛紛撰文紀念,不禁想起了自己的高考。就那次考試來說,成績還不錯,但因為填報的學校比較保...
    如心1976阅读 1,631评论 6 2