码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——悬浮按钮

时间:2016-06-03 15:44:51      阅读:722      评论:0      收藏:0      [点我收藏+]

标签:

项目中需要在创建一个悬浮按钮,自己觉得光创建一个按钮不能滑动有点不太优化,就自己试着做了一个可以随意拖动的悬浮按钮,希望大家能够多多支持。

 

  -(void)viewDidLoad
{
//创建悬浮按钮
    self.editEventsButton=[UIButton  buttonWithType:UIButtonTypeCustom];
    self.editEventsButton.frame=CGRectMake(0, 0, 60, 60);
    [self.editEventsButton  setBackgroundImage:[UIImage  imageNamed:@"问医生"] forState:UIControlStateNormal];
    [self.editEventsButton  addTarget:self action:@selector(gotoEditEvents) forControlEvents:UIControlEventTouchUpInside];
    self.window=[[UIWindow  alloc] initWithFrame:CGRectMake(ScreenWidth-55, ScreenHeight-75-CGRectGetHeight(self.chartView.frame), 60, 60)];
    self.window.windowLevel=UIWindowLevelAlert+1;
    self.window.backgroundColor=[UIColor  clearColor];
    self.window.layer.cornerRadius=20;
    self.window.layer.masksToBounds=YES;
    [self.window  addSubview:self.editEventsButton];
    
    [self.window  makeKeyAndVisible];
  //放一个拖动手势,用来改变控件的位置
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];
    [self.editEventsButton addGestureRecognizer:pan];
    [self  charts];
    [self  tabarTool];
}
//手势事件 -- 改变位置
-(void)changePostion:(UIPanGestureRecognizer *)pan
{
    CGPoint point = [pan translationInView:self.window];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGRect originalFrame = self.window.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;
    }
    self.window.frame = originalFrame;
    [pan setTranslation:CGPointZero inView:self.view];
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        _editEventsButton.enabled = NO;
    }else if (pan.state == UIGestureRecognizerStateChanged){
        
    } else {
        
        CGRect frame = self.window.frame;
        //记录是否越界
        BOOL isOver = NO;
        
        if (frame.origin.x < 0) {
            frame.origin.x = 0;
            isOver = YES;
        } else if (frame.origin.x+frame.size.width > width) {
            frame.origin.x = width - frame.size.width;
            isOver = YES;
        }
        
        if (frame.origin.y < 0) {
            frame.origin.y = 0;
            isOver = YES;
        } else if (frame.origin.y+frame.size.height > height) {
            frame.origin.y = height - frame.size.height;
            isOver = YES;
        }
        if (isOver) {
            [UIView animateWithDuration:0.3 animations:^{
                self.window.frame = frame;
            }];
        }
        _editEventsButton.enabled = YES;
    }
}

 

iOS开发——悬浮按钮

标签:

原文地址:http://www.cnblogs.com/qinxiaoguang/p/5556462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!