美文网首页IOS
自定义圆环,可转动,可点击,中间可自定义图片

自定义圆环,可转动,可点击,中间可自定义图片

作者: 迷恋代码 | 来源:发表于2017-03-06 14:24 被阅读115次

    一个自定义的圆环,可转动,给每个按钮添加跳转或者事件,自己加了一些点击时播放系统的声音和振动,直接上代码,粘贴复制可用:


    .h文件:

    .m文件:

    定义的变量

    #pragma mark -  加子视图

    - (void)addSubviewWithSubView:(NSArray *)imageArray andTitle:(NSArray *)titleArray andSize:(CGSize)size andCenterImage:(UIImage *)centerImage

    {

    _subViewSize=size;

    _numOfSubView = (CGFloat)titleArray.count;

    btnArray = [[NSMutableArray alloc]init];

    for(NSInteger i=0; i<_numOfSubView ;i++){

    UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(20*Width, 20*Width, size.width*Width, size.height*Height)];

    [button setImage:imageArray[i] forState:UIControlStateNormal];

    //设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素

    [button setTitle:titleArray[i] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:16*Width];

    button.titleLabel.textAlignment = NSTextAlignmentCenter;

    CGFloat devcide = KScreenHeight;

    //        button.imageEdgeInsets = UIEdgeInsetsMake(6*Width,6*Width,10*Width,button.titleLabel.bounds.size.width);

    if (devcide == 568.000000) {

    button.imageEdgeInsets = UIEdgeInsetsMake(6,6,8,button.titleLabel.bounds.size.width);

    button.titleEdgeInsets = UIEdgeInsetsMake(55, -button.imageView.bounds.size.width-50, 0, 8);

    }else if (devcide == 480.000000){

    button.imageEdgeInsets = UIEdgeInsetsMake(2,10,5,button.titleLabel.bounds.size.width);

    button.titleEdgeInsets = UIEdgeInsetsMake(55, -button.imageView.bounds.size.width-50, 2, 5);

    }else if (devcide == 736.000000){

    button.imageEdgeInsets = UIEdgeInsetsMake(8,8,8,button.titleLabel.bounds.size.width);

    button.titleEdgeInsets = UIEdgeInsetsMake(70, -button.imageView.bounds.size.width-40, 0, 0);

    }else{

    button.imageEdgeInsets = UIEdgeInsetsMake(5,6,8,button.titleLabel.bounds.size.width);

    button.titleEdgeInsets = UIEdgeInsetsMake(60, -button.imageView.bounds.size.width-45, 0, 0);

    }

    [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

    button.tag=100+i;

    [btnArray addObject:button];

    [_subViewArray addObject:button];

    [_circleView addSubview:button];

    }

    [self layoutBtn];

    //中间视图

    UIButton *buttonCenter=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/3.0, self.frame.size.height/3.0)];

    buttonCenter.tag=100+_numOfSubView+1;

    buttonCenter.layer.cornerRadius=self.frame.size.width/6.0;

    //    [buttonCenter setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

    [buttonCenter setImage:centerImage forState:UIControlStateNormal];

    buttonCenter.center=CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

    [buttonCenter addTarget:self action:@selector(clickBtnCenter:) forControlEvents:UIControlEventTouchUpInside];

    [_subViewArray addObject:buttonCenter];

    [_circleView addSubview:buttonCenter];

    //加转动手势

    _pgr=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(zhuanPgr:)];

    [_circleView addGestureRecognizer:_pgr];

    //加点击效果

    for (NSInteger i=0; i<_subViewArray.count; i++) {

    UIButton *button=_subViewArray[i];

    [button addTarget:self action:@selector(subViewOut:) forControlEvents:UIControlEventTouchUpInside];

    }

    }

    - (void)clickBtn:(UIButton *)btn

    {

    NSLog(@"%zd圆环按钮点击了",btn.tag);

    kaiduImage.alpha = 1;

    [UIView animateWithDuration:2.0 animations:^{

    kaiduImage.alpha = 0;

    } completion:^(BOOL finished) {

    }];

    if (btn.tag == 100) {

    mStartAngle = M_PI_2*3;

    [self layoutBtn];

    }

    if (btn.tag == 101) {

    mStartAngle = M_PI_2*2.32;

    [self layoutBtn];

    }

    if (btn.tag == 102) {

    mStartAngle = M_PI_2*1.68;

    [self layoutBtn];

    }

    if (btn.tag == 103) {

    mStartAngle = M_PI_2*1;

    [self layoutBtn];

    }

    if (btn.tag == 104) {

    mStartAngle = M_PI_2*0.32;

    [self layoutBtn];

    }

    if (btn.tag == 105) {

    //        mStartAngle = M_PI_2*3.66;

    mStartAngle = -M_PI_2*0.32;

    [self layoutBtn];

    }

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    if ([user boolForKey:@"voice"]) {

    //播放音效

    //1.获得音效文件的全路径

    NSURL *soundUrl = [[NSBundle mainBundle] URLForResource:@"click.wav" withExtension:nil];

    //2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundUrl, &soundID);

    //把需要销毁的音效文件的ID传递给它既可销毁

    AudioServicesPlaySystemSound(soundID);

    }

    if ([user boolForKey:@"vibration"]) {

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    }

    }

    //点击圆心按钮

    - (void)clickBtnCenter:(UIButton *)btnCenter

    {

    NSLog(@"圆心按钮点击了");

    }

    //按钮布局

    -(void)layoutBtn{

    for (NSInteger i=0; i<_numOfSubView ;i++) {// 178,245

    CGFloat devcide = KScreenHeight;

    if (devcide == 480.000000) {

    CGFloat yy=145+sin((i/_numOfSubView)*M_PI_2*4+mStartAngle)*(self.frame.size.width/2-_subViewSize.width/2-20);

    CGFloat xx=148+cos((i/_numOfSubView)*M_PI_2*4+mStartAngle)*(self.frame.size.width/2-_subViewSize.width/2-20);

    UIButton *button=[btnArray objectAtIndex:i];

    button.center=CGPointMake(xx, yy);

    }else if (devcide == 736.000000){

    CGFloat yy=195+sin((i/_numOfSubView)*M_PI_2*4+mStartAngle)*(self.frame.size.width/2-_subViewSize.width/2-30);

    CGFloat xx=195+cos((i/_numOfSubView)*M_PI_2*4+mStartAngle)*(self.frame.size.width/2-_subViewSize.width/2-30);

    UIButton *button=[btnArray objectAtIndex:i];

    button.center=CGPointMake(xx, yy);

    }else{

    CGFloat yy=175*Height+sin((i/_numOfSubView)*M_PI*2+mStartAngle)*(self.frame.size.width/2-_subViewSize.width/2-20*Height);

    CGFloat xx=175*Width+cos((i/_numOfSubView)*M_PI*2+mStartAngle)*(self.frame.size.width/2-_subViewSize.width/2-20*Width);

    UIButton *button=[btnArray objectAtIndex:i];

    button.center=CGPointMake(xx, yy);

    }

    }

    }

    NSTimer *flowtime;

    NSTimer *reverseTime;

    float anglePerSecond;

    float speed;  //转动速度

    #pragma mark - 转动手势

    -(void)zhuanPgr:(UIPanGestureRecognizer *)pgr

    {

    if(pgr.state==UIGestureRecognizerStateBegan){

    kaiduImage.alpha = 1.0;

    mTmpAngle = 0;

    beginPoint=[pgr locationInView:self];

    startTouchDate=[NSDate date];

    }else if (pgr.state==UIGestureRecognizerStateChanged){

    float StartAngleLast = mStartAngle;

    movePoint= [pgr locationInView:self];

    float start = [self getAngle:beginPoint];  //获得起始弧度

    float end = [self getAngle:movePoint];    //结束弧度

    if ([self getQuadrant:movePoint] == 1 || [self getQuadrant:movePoint] == 4) {

    mStartAngle += end - start;

    mTmpAngle += end - start;

    //            NSLog(@"第一、四象限____%f",mStartAngle);

    } else

    // 二、三象限,色角度值是付值

    {

    mStartAngle += start - end;

    mTmpAngle += start - end;

    //            NSLog(@"第二、三象限____%f",mStartAngle);

    //            NSLog(@"mTmpAngle is %f",mTmpAngle);

    }

    [self layoutBtn];

    beginPoint=movePoint;

    speed = mStartAngle - StartAngleLast;

    NSTimeInterval time=[[NSDate date] timeIntervalSinceDate:startTouchDate];

    anglePerSecond = mTmpAngle*50/ time;

    }else if (pgr.state==UIGestureRecognizerStateEnded){

    // 计算,每秒移动的角度

    NSTimeInterval time=[[NSDate date] timeIntervalSinceDate:startTouchDate];

    anglePerSecond = mTmpAngle*50/ time;

    // 如果达到该值认为是快速移动

    if (anglePerSecond > 0) {

    if (fabsf(anglePerSecond) > mFlingableValue && !_isPlaying) {//顺时针

    // post一个任务,去自动滚动

    _isPlaying = true;

    flowtime = [NSTimer scheduledTimerWithTimeInterval:0.1

    target:self

    selector:@selector(flowAction)

    userInfo:nil

    repeats:YES];

    }

    }else{

    if (-fabsf(anglePerSecond) < -mFlingableValue && !_isPlaying) {//逆时针

    _isPlaying = true;

    reverseTime = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(reverseAction) userInfo:nil repeats:YES];

    }

    }

    NSLog(@"%f-----!!!!!!!!!!!",mStartAngle);

    //wxb 非快速滑动停止时

    if (_isPlaying==false)

    {

    if (mStartAngle>=0)

    {

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*2.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*3+M_PI_2*0.32) {

    mStartAngle = M_PI_2*3;

    [self layoutBtn];

    self.clickSomeOne(@"100");

    }

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*1.68+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*2.32+M_PI_2*0.32) {

    mStartAngle = M_PI_2*2.32;

    [self layoutBtn];

    self.clickSomeOne(@"101");

    }

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*1+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*1.68+M_PI_2*0.32) {

    mStartAngle = M_PI_2*1.68;

    [self layoutBtn];

    self.clickSomeOne(@"102");

    }

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*0.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*1+M_PI_2*0.32) {

    mStartAngle = M_PI_2*1;

    [self layoutBtn];

    self.clickSomeOne(@"103");

    }

    if (fmod(mStartAngle, M_PI*2)>0&&fmod(mStartAngle, M_PI*2)<=M_PI_2*0.32+M_PI_2*0.32) {

    mStartAngle = M_PI_2*0.32;

    [self layoutBtn];

    //                    self.clickSomeOne(@"104");

    }

    if (fmod(mStartAngle, M_PI*2)-M_PI*2<=0&&fmod(mStartAngle, M_PI*2)-M_PI*2>=-M_PI_2*0.32*2) {

    //        mStartAngle = M_PI_2*3.66;

    mStartAngle = -M_PI_2*0.32;

    [self layoutBtn];

    self.clickSomeOne(@"105");

    }

    }

    else

    {

    if (fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*2.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*3+M_PI_2*0.32) {

    mStartAngle = M_PI_2*3;

    [self layoutBtn];

    self.clickSomeOne(@"100");

    }

    if (fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*1.68+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*2.32+M_PI_2*0.32) {

    mStartAngle = M_PI_2*2.32;

    [self layoutBtn];

    self.clickSomeOne(@"101");

    }

    if (fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*1+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*1.68+M_PI_2*0.32) {

    mStartAngle = M_PI_2*1.68;

    [self layoutBtn];

    self.clickSomeOne(@"102");

    }

    if (fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*0.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*1+M_PI_2*0.32) {

    mStartAngle = M_PI_2*1;

    [self layoutBtn];

    self.clickSomeOne(@"103");

    }

    if (fmod(mStartAngle, M_PI*2)+M_PI*2>0+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*0.32+M_PI_2*0.32) {

    mStartAngle = M_PI_2*0.32;

    [self layoutBtn];

    //                    self.clickSomeOne(@"104");

    }

    if (fmod(mStartAngle, M_PI*2)<=0&&fmod(mStartAngle, M_PI*2)>=-M_PI_2*0.32*2) {

    //        mStartAngle = M_PI_2*3.66;

    mStartAngle = -M_PI_2*0.32;

    [self layoutBtn];

    self.clickSomeOne(@"105");

    }

    }

    }

    }

    }

    - (void)beginPlayMusic

    {

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    if ([user boolForKey:@"voice"]) {

    //播放音效

    //1.获得音效文件的全路径

    NSURL *soundUrl = [[NSBundle mainBundle] URLForResource:@"swipe.mp3" withExtension:nil];

    //2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundUrl, &soundID);

    //把需要销毁的音效文件的ID传递给它既可销毁

    AudioServicesPlaySystemSound(soundID);

    }

    }

    //获取当前点弧度

    -(float)getAngle:(CGPoint)point {

    double x = point.x - mRadius;

    double y = point.y - mRadius;

    return (float) (asin(y / hypot(x, y)));

    }

    /**

    * 根据当前位置计算象限

    *

    * @param x

    * @param y

    * @return

    */

    -(int) getQuadrant:(CGPoint) point {

    int tmpX = (int) (point.x - mRadius);

    int tmpY = (int) (point.y - mRadius);

    if (tmpX >= 0) {

    return tmpY >= 0 ? 1 : 4;

    } else {

    return tmpY >= 0 ? 2 : 3;

    }

    }

    -(void)flowAction{

    if (speed < 0.1) {

    [UIView animateWithDuration:2 animations:^{

    kaiduImage.alpha = 0;

    } completion:^(BOOL finished) {

    }];

    _isPlaying = false;

    [flowtime invalidate];

    flowtime = nil;

    //wxb

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*2.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*3+M_PI_2*0.32) {

    mStartAngle = M_PI_2*3;

    [self layoutBtn];

    self.clickSomeOne(@"100");

    }

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*1.68+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*2.32+M_PI_2*0.32) {

    mStartAngle = M_PI_2*2.32;

    [self layoutBtn];

    self.clickSomeOne(@"101");

    }

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*1+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*1.68+M_PI_2*0.32) {

    mStartAngle = M_PI_2*1.68;

    [self layoutBtn];

    self.clickSomeOne(@"102");

    }

    if (fmod(mStartAngle, M_PI*2)>M_PI_2*0.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*1+M_PI_2*0.32) {

    mStartAngle = M_PI_2*1;

    [self layoutBtn];

    self.clickSomeOne(@"103");

    }

    if (fmod(mStartAngle, M_PI*2)>0&&fmod(mStartAngle, M_PI*2)<=M_PI_2*0.32+M_PI_2*0.32) {

    mStartAngle = M_PI_2*0.32;

    [self layoutBtn];

    //            self.clickSomeOne(@"104");

    }

    if (fmod(mStartAngle, M_PI*2)-M_PI*2<=0&&fmod(mStartAngle, M_PI*2)-M_PI*2>=-M_PI_2*0.32*2) {

    //        mStartAngle = M_PI_2*3.66;

    mStartAngle = -M_PI_2*0.32;

    [self layoutBtn];

    self.clickSomeOne(@"105");

    }

    return;

    }

    // 不断改变mStartAngle,让其滚动,/30为了避免滚动太快

    mStartAngle += speed ;

    speed = speed/1.1;

    // 逐渐减小这个值

    //    anglePerSecond /= 1.1;

    NSLog(@"++ %f--%f",mStartAngle,speed);

    [self layoutBtn];

    }

    - (void)reverseAction

    {

    //    NSLog(@"%f",speed);

    if (speed > -0.1) {

    [UIView animateWithDuration:2 animations:^{

    kaiduImage.alpha = 0;

    } completion:^(BOOL finished) {

    }];

    _isPlaying = false;

    [reverseTime invalidate];

    reverseTime = nil;

    //wxb

    if ((fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*2.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*3+M_PI_2*0.32)||(fmod(mStartAngle, M_PI*2)>M_PI_2*2.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*3+M_PI_2*0.32)) {

    mStartAngle = M_PI_2*3;

    [self layoutBtn];

    self.clickSomeOne(@"100");

    }

    if ((fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*1.68+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*2.32+M_PI_2*0.32)||(fmod(mStartAngle, M_PI*2)>M_PI_2*1.68+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*2.32+M_PI_2*0.32)) {

    mStartAngle = M_PI_2*2.32;

    [self layoutBtn];

    self.clickSomeOne(@"101");

    }

    if ((fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*1+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*1.68+M_PI_2*0.32)||(fmod(mStartAngle, M_PI*2)>M_PI_2*1+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*1.68+M_PI_2*0.32)) {

    mStartAngle = M_PI_2*1.68;

    [self layoutBtn];

    self.clickSomeOne(@"102");

    }

    if ((fmod(mStartAngle, M_PI*2)+M_PI*2>M_PI_2*0.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*1+M_PI_2*0.32)||(fmod(mStartAngle, M_PI*2)>M_PI_2*0.32+M_PI_2*0.32&&fmod(mStartAngle, M_PI*2)<=M_PI_2*1+M_PI_2*0.32)) {

    mStartAngle = M_PI_2*1;

    [self layoutBtn];

    self.clickSomeOne(@"103");

    }

    if ((fmod(mStartAngle, M_PI*2)+M_PI*2>0&&fmod(mStartAngle, M_PI*2)+M_PI*2<=M_PI_2*0.32+M_PI_2*0.32)||(fmod(mStartAngle, M_PI*2)>0&&fmod(mStartAngle, M_PI*2)<=M_PI_2*0.32+M_PI_2*0.32)) {

    mStartAngle = M_PI_2*0.32;

    [self layoutBtn];

    //            self.clickSomeOne(@"104");

    }

    if (fmod(mStartAngle, M_PI*2)<=0&&fmod(mStartAngle, M_PI*2)>=-M_PI_2*0.32*2) {

    //        mStartAngle = M_PI_2*3.66;

    mStartAngle = -M_PI_2*0.32;

    [self layoutBtn];

    self.clickSomeOne(@"105");

    }

    return;

    }

    mStartAngle += speed;

    speed = speed/1.1;

    [self layoutBtn];

    }

    //按钮的点击回调

    -(void)subViewOut:(UIButton *)button

    {

    //点击

    if(self.clickSomeOne){

    self.clickSomeOne([NSString stringWithFormat:@"%ld",(long)button.tag]);

    }

    //    NSLog(@"快点播放,别要点我");

    }

    相关文章

      网友评论

        本文标题:自定义圆环,可转动,可点击,中间可自定义图片

        本文链接:https://www.haomeiwen.com/subject/roxugttx.html