欢迎光临
我们一直在努力

简单的可拖动的弹出button

建站超值云服务器,限时71元/月

简单的可拖动的弹出button

项目介绍:

在别人的基础上进行了改进,简化了代码,新增了拖动功能,改变了动画方式。

ViewController.m 的实现方法:
[Objective-C] 查看源文件 复制代码

- (void)createSmallBtnWithNumber:(int)number gap:(CGFloat)gap centerDistance:(CGFloat)distance {
    for (int i = 0; i < number; i ++) {
        CGFloat angle = M_PI * 2 / number;
        CGFloat rad = distance * sinf(angle / 2) - gap / 2.0;
        ang = angle;
        centerDis = distance;
        num = number;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect rect = { {0,0},{2 * rad, 2 * rad} };
        btn.frame = rect;
        btn.tag = 100 + i;
        btn.alpha = 0;
        btn.layer.cornerRadius = rad;
        [btn setTitle:[NSString stringWithFormat:@"第%d个",i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"bg1"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"bg2"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(bunClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.center = self.view.center;
        [self.view addSubview:btn];
        btns = [NSMutableArray array];
        [btns addObject:btn];
    }
}

- (void)bunClick:(UIButton *)btn {
    for (int i = 0; i < num; i ++) {
        if (i == btn.tag - 100) {
            btn.selected = YES;
        }
        else {
            UIButton *button = (UIButton *)[self.view viewWithTag:i + 100];
            button.selected = NO;
        }
    }
}


- (IBAction)centerButton:(UIButton *)sender {
    for (int i = 0; i < num; i ++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:100 + i];
        CGFloat x = centerDis * cosf(ang * i) + sender.center.x;
        CGFloat y = centerDis * sinf(ang * i) + sender.center.y;
        [UIView animateWithDuration:0.3 delay:0.1 * i options:UIViewAnimationOptionCurveLinear animations:^{
            if (!CGPointEqualToPoint(btn.center, sender.center)) {
                UIButton *btn = (UIButton *)[self.view viewWithTag:100 + i];
                btn.center = sender.center;
                btn.alpha = 0;
                sender.selected = NO;
            }
            else {
                btn.center = CGPointMake(x, y);
                btn.alpha = 1.0;
                sender.selected = YES;
            }
        } completion:nil];
    }
}
- (IBAction)moveAction:(UIPanGestureRecognizer *)sender {
    CGPoint point = [sender translationInView:self.view];
    sender.view.center = CGPointMake(sender.view.center.x + point.x, sender.view.center.y + point.y);
    [sender setTranslation:CGPointZero inView:self.view];
    UIButton *centerBtn = (UIButton *)sender.view;
    for (int i = 0; i < num; i ++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:100 + i];
        CGFloat x = centerDis * cosf(ang * i) + centerBtn.center.x;
        CGFloat y = centerDis * sinf(ang * i) + centerBtn.center.y;
        if (centerBtn.selected) {
            btn.center = CGPointMake(x, y);
        }
        else {
            btn.center = centerBtn.center;
        }
    }
}



DefineButton.zip
(181.96 KB, 下载次数: 868)

2016-8-8 10:18 上传
点击文件名下载附件

CCP_Code

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 简单的可拖动的弹出button
分享到: 更多 (0)