iOS 圆形旋转托盘

代码下载:http://pan.baidu.com/s/1sZb3g


效果图:


DJItemBtn.h

//
//  DJItemBtn.h
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-25.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@protocol DJItemBtnDelegate <NSObject>
@optional

-(void)djItemMethod:(int)btnTag;
@end
@interface DJItemBtn : UIView
{
@public
   // id<DJItemBtnDelegate>delegate;
}

@property (retain,nonatomic)    id<DJItemBtnDelegate>delegate;
    


@end

DJItemBtn.m

//
//  DJItemBtn.m
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-25.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import "DJItemBtn.h"
#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180

#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f)


@implementation DJItemBtn

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       self.backgroundColor = [UIColor greenColor];
       
        self.layer.anchorPoint = CGPointMake(0, 0.5);
       
 
        UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiaonan1.jpg"]];
        NSLog(@"aaaaaframe.size.width = %f",frame.size.width);
        imageView.frame = CGRectMake(frame.size.width / 2, 0, 50, 25);
        
        [self addSubview:imageView];
        imageView.userInteractionEnabled = true;
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBtn)];
        [imageView addGestureRecognizer:tap];

        
        
    }
    return self;
}
-(void)clickBtn
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"calculateDegress" object:nil];
    
    [_delegate djItemMethod:self.tag];
    NSLog(@"%d",self.tag);
    
}




@end

DJRotateView.h

//
//  DJRotateView.h
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-25.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DJItemBtn.h"
@interface DJRotateView : UIView

-(id)initWithView:(CGRect)frame addtarget:(id)delegate;

@end

DJRotateView.m

//
//  DJRotateView.m
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-25.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import "DJRotateView.h"


#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180

#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f)



@implementation DJRotateView
-(id)initWithView:(CGRect)frame addtarget:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        //self.backgroundColor = [UIColor redColor];
        float segment = 7.0;
        double degree = 360 / segment;
        float rotate =  CC_DEGREES_TO_RADIANS(degree);
         NSLog(@"frame.size.width = %f",degree);
        for (float i = 1.0; i <= 7.0; i++) {
            DJItemBtn* djItemBtn = [[DJItemBtn alloc] initWithFrame:CGRectMake(frame.size.width / 2  - 50, frame.size.height / 2 * 0.87, 100, 25)];
            djItemBtn.delegate = delegate;
            [djItemBtn setTag:i];
            
            djItemBtn.layer.transform = CATransform3DMakeRotation(rotate * (float)i , 0, 0, 1);
            NSLog(@"rotate *i = %f",180.000 - degree *i);
            NSLog(@"degree * i = %f",degree * i);
            [self addSubview:djItemBtn];  
        }
    }
    return self;
}
//-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
//{
//    return YES;
//}

//-(void)setRotate:(float)degress
//{
//    float rotate = CC_DEGREES_TO_RADIANS(degress);
//    CGAffineTransform transform = self.djrotateView.transform;
//    transform = CGAffineTransformRotate(transform, rotate);
//    self.djrotateView.transform = transform;
//    
//}
@end


HLRotateMenuView.h

//
//  HLRotateMenuView.h
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-28.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DJRotateView.h"
@interface HLRotateMenuView : UIView
{
    float angle;
    float rotate;
    float nowDegress;
    float previousDegress;
    //计算角度变化的数据
    float degressNum;
    //托盘中按钮的个数
    float djSegmentNum;
    //按钮间的夹角
    float djDegressBtn;
    
    //之前的角度
    float beforeDegree;
    //当前的角度
    float currentDegree;
    
    
    
}
@property (strong,nonatomic) DJRotateView* djrotateView;
@property (assign, nonatomic) BOOL selectBool;
@property (assign,nonatomic) CGPoint latestLocation;
-(id)initWithView:(CGRect)frame addtarget:(id)delegate;
@end

HLRotateMenuView.m

//
//  HLRotateMenuView.m
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-28.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import "HLRotateMenuView.h"
#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180

#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f)


float djDistance(const CGPoint v1, const CGPoint v2)
{
    float tempX = v1.x - v2.x;
    float tempY = v1.y - v2.y;
    return sqrtf(tempX * tempX + tempY * tempY);
}
@implementation HLRotateMenuView


-(id)initWithView:(CGRect)frame addtarget:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        //self.layer.transform = CATransform3DMakeRotation(180 , 0, 0, 1);

        
        
        self.djrotateView = [[DJRotateView alloc] initWithView:CGRectMake(0, 0, frame.size.width, frame.size.height) addtarget:delegate];
        

        [self addSubview:self.djrotateView];
        djSegmentNum = 7;
        djDegressBtn = 360 / djSegmentNum;
        degressNum = 180 - djDegressBtn * 1;
        //给当前角度赋值
        currentDegree  = 180 - djDegressBtn * 1;
        //给之前的角度赋值
        beforeDegree = 180 - djDegressBtn * 2;
        NSLog(@"beforeDegree = %f",beforeDegree);
        
        [self setRotate:180 - djDegressBtn * 2];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calculateDegress) name:@"calculateDegress" object:nil];
     
    }
    return self;
}

-(void)djAddBtn
{
    
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    self.latestLocation = [touch locationInView:self];
    float distance = djDistance(self.latestLocation, self.djrotateView.center);
    
    if (distance < 150) {
        self.selectBool = true;
    }
    else
    {
        self.selectBool = true;
    }
    
    if (distance < 28) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reduction" object:nil];
    }
    
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.selectBool) {
        
        UITouch* touch = [touches anyObject];
        
        CGPoint previousLocation = [touch previousLocationInView:self];
        
        CGPoint previouscgp = [self cgpSub:previousLocation :self.djrotateView.center];
        float previousVector = [self toAngle:previouscgp];
        
        previousDegress = CC_RADIANS_TO_DEGREES(previousVector);
        
        CGPoint nowLocation = [touch locationInView:self];
        CGPoint nowcgp = [self cgpSub:nowLocation :self.djrotateView.center];
        float nowVector = [self toAngle:nowcgp];
        nowDegress = CC_RADIANS_TO_DEGREES(nowVector);
       
        //在这里判断角度
        angle = -(nowDegress - previousDegress);
        degressNum += angle;
        
 
        NSLog(@"%f",degressNum);
        
        [self setRotate:angle];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSLog(@"degressNum = %f",degressNum - beforeDegree);
    [self calculateDegress];
      NSLog(@"degressNum = %f",degressNum);
   
    
}
-(void)calculateDegress
{
    
//    float tempAngleNum = degressNum;

    // if (degressNum > 0) {
    if (degressNum / 360 >= 1 || degressNum / 360 <= -1)
    //if (degressNum >= 1 || degressNum <= -1)
    {
//        NSString* tempStr = [NSString stringWithFormat:@"%f",degressNum / 360];
//        NSArray* tempArr = [tempStr componentsSeparatedByString:@"."];
//        
//       // NSLog(@"%@",[tempArr objectAtIndex:0]);
//        NSString* temp = [tempArr objectAtIndex:0];
//        tempAngleNum = degressNum - 360 * [temp intValue];
    }
    else
    {
        //tempAngleNum = degressNum;
    }
    //求角度偏差
    
    //NSString* subAngleStr = [NSString stringWithFormat:@"%f",tempAngleNum / djDegressBtn];
    //  NSString* subAngleStr = [NSString stringWithFormat:@"%f", (degressNum - beforeDegree) / djDegressBtn];
   
    //NSArray* subArr = [subAngleStr componentsSeparatedByString:@"."];
    
    float fDegree = (degressNum - beforeDegree) / djDegressBtn;
    int tempSubAngle = fDegree / 1;
    
    
    //int tempSubAngle = [[subArr objectAtIndex:0] intValue];
    
    
    NSLog(@"degressNum = %d",tempSubAngle);
//    NSLog(@"angle = %f",tempAngleNum);
//    NSLog(@"Num = %f",tempAngleNum - (djDegressBtn * tempSubAngle));
    //需要转角的差值
//    float offAngle = tempAngleNum - (djDegressBtn * tempSubAngle);
    float offAngle = (degressNum - beforeDegree) - (djDegressBtn * tempSubAngle);
    
    NSLog(@"魔术11 = %f",offAngle);
    if (offAngle > 0)
    {
        if (offAngle < djDegressBtn / 2)
        {
            offAngle = - offAngle;
        }
        else if(offAngle > djDegressBtn / 2)
        {
            offAngle = djDegressBtn - offAngle;
        }
    }
    else
    {
        if (offAngle > -djDegressBtn / 2)
        {
            offAngle = -offAngle;
        }
        else
        {
            offAngle = -djDegressBtn - offAngle;
        }
    }
    
    NSLog(@"魔术 = %f",offAngle);
    degressNum += offAngle;
    
    // [self setRotate:offAngle];
    [self setRotateAnimation:offAngle];
}



- (CGPoint) cgpSub:(CGPoint)v1: (CGPoint)v2
{
    CGPoint point;
    point.x = v1.x - v2.x;
    point.y = v1.y - v2.y;
    
    return point;
}

-(float)toAngle:(CGPoint)v
{
    return atan2f(v.x, v.y);
}


-(void)setRotate:(float)degress
{
    rotate = CC_DEGREES_TO_RADIANS(degress);
    CGAffineTransform transform = self.djrotateView.transform;
    transform = CGAffineTransformRotate(transform, rotate);
    self.djrotateView.transform = transform;
    
}
-(void)setRotateAnimation:(float)degress
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    
    
    rotate = CC_DEGREES_TO_RADIANS(degress);
    CGAffineTransform transform = self.djrotateView.transform;
    transform = CGAffineTransformRotate(transform, rotate);
    self.djrotateView.transform = transform;
    [UIView commitAnimations];
}

@end

HLRotateMenu.h

//
//  HLRotateMenu.h
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-30.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "HLRotateMenuView.h"
@interface HLRotateMenu : UIView
@property (strong, nonatomic) HLRotateMenuView* hlRotateMenuView;
@property (strong, nonatomic) UIImageView* btnImage;

-(id)initWithView:(CGRect)frame addtarget:(id)delegate;
@end

HLRotateMenu.m

//
//  HLRotateMenu.m
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-30.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#define IMAGE_WIDTH 35
#define IMAGE_HEIGHT 40
#import "HLRotateMenu.h"

@implementation HLRotateMenu


-(id)initWithView:(CGRect)frame addtarget:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        self.hlRotateMenuView = [[HLRotateMenuView alloc] initWithView:CGRectMake(100, 0, frame.size.width, frame.size.height) addtarget:delegate];
        self.hlRotateMenuView.alpha = 0.0;
        [self addSubview:self.hlRotateMenuView];
        
        self.btnImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"changmen.jpg"]];
        self.btnImage.frame = CGRectMake(frame.size.width / 2 - 10-10, frame.size.height / 2 - 15, IMAGE_WIDTH, IMAGE_HEIGHT);
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnClick)];
        self.btnImage.userInteractionEnabled = YES;
        
        [self.btnImage addGestureRecognizer:tap];
        [self addSubview:self.btnImage];
        
        
        
       
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reduction) name:@"reduction" object:nil];
        
    }
    return self;
}

-(void)btnClick
{
    [UIView animateWithDuration:0.3 animations:^{
        self.btnImage.alpha = 0.0;
        self.hlRotateMenuView.alpha = 1.0;
        self.btnImage.frame = CGRectMake(self.hlRotateMenuView.frame.size.width / 2 - 10 - 25 , self.hlRotateMenuView.frame.size.height / 2, IMAGE_WIDTH, IMAGE_HEIGHT);
        self.hlRotateMenuView.frame = CGRectMake(0, 0, 200, 200);
        
    } completion:^(BOOL finished) {
        
    }];
}


//还原
-(void)reduction
{
    [UIView animateWithDuration:0.3 animations:^{
        self.btnImage.alpha = 1.0;
        self.hlRotateMenuView.alpha = 0.0;
        self.btnImage.frame = CGRectMake(self.hlRotateMenuView.frame.size.width / 2 - 10- 10 , self.hlRotateMenuView.frame.size.height / 2, IMAGE_WIDTH, IMAGE_HEIGHT);
        self.hlRotateMenuView.frame = CGRectMake(100, 0, 200, 200);
        
    } completion:^(BOOL finished) {
        
    }];
}

@end

ViewController.h

//
//  ViewController.h
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-25.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "HLRotateMenuView.h"
#import "HLRotateMenu.h"

@interface ViewController : UIViewController<DJItemBtnDelegate>
@property (strong, nonatomic) HLRotateMenuView* hlRotateMenuView;
@property (strong, nonatomic) HLRotateMenu* hlRotateMenu;

@end

ViewController.m

//
//  ViewController.m
//  DjRotateMenuForiOS
//
//  Created by 杜甲 on 13-9-25.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import "ViewController.h"




@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];



//    self.hlRotateMenuView = [[HLRotateMenuView alloc] initWithView:CGRectMake(self.view.frame.size.width - 250, 100, 200, 200) addtarget:self];
//    [self.view addSubview:self.hlRotateMenuView];
    
    self.hlRotateMenu = [[HLRotateMenu alloc] initWithView:CGRectMake(self.view.frame.size.width - 100, 100, 200, 200)  addtarget:self];
    [self.view addSubview:self.hlRotateMenu];
}




-(void)djItemMethod:(int)btnTag
{
    NSLog(@"%d",btnTag);
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值