登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

简单的可拖动的弹出button

[复制链接]
来自: ccp_code4 分类: iOS精品源码 上传时间: 2016-8-8 10:18:33
Tag:按钮 button 动画 拖拽 animation

项目介绍:

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

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, 下载次数: 794)
1857 2 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表