您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 圓形旋轉托盤菜單的實現

編輯:iOS 開發綜合

這是一個可以拖動旋轉的圓形托盤菜單,效果圖如下:

整個實現由幾個類協作完成:DJItemBtn 是托盤上的單個按鈕,DJRotateView 負責按角度均勻排布這些按鈕,HLRotateMenuView 處理拖動旋轉與角度吸附,HLRotateMenu 則封裝了展開/收起的動畫。下面挑出關鍵程式碼說明。

DJItemBtn:托盤按鈕

每個按鈕把自己的 anchorPoint 設為 (0, 0.5),這樣旋轉時才會繞著圓心轉動。點擊時同時發出通知與 delegate 回調:

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

- (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"]];
        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];
}

DJRotateView:均勻排布按鈕

把 360 度按按鈕數量均分,再用 CATransform3DMakeRotation 讓每個按鈕轉到各自的角度:

-(id)initWithView:(CGRect)frame addtarget:(id)delegate {
    self = [super initWithFrame:frame];
    if (self) {
        float segment = 7.0;
        double degree = 360 / segment;
        float rotate = CC_DEGREES_TO_RADIANS(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);
            [self addSubview:djItemBtn];
        }
    }
    return self;
}

HLRotateMenuView:拖動旋轉與角度吸附

核心在於計算手指移動前後、相對於圓心的角度差,並累加到總旋轉角度上。以 atan2f 求角度,用兩點距離判斷是否觸發旋轉:

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);
}

-(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;
        [self setRotate:angle];
    }
}

-(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;
}

手指離開時(touchesEnded)會呼叫 calculateDegress,計算當前的角度偏差,並用 setRotateAnimation: 以 0.2 秒動畫把托盤「吸附」到最近的按鈕位置,帶來Like實用手指撥動轉盤的手感。

HLRotateMenu:展開與收起

最外層用一個開關圖示控制托盤的展開與還原,透過 alpha 與 frame 的動畫過渡:

-(void)btnClick {
    [UIView animateWithDuration:0.3 animations:^{
        self.btnImage.alpha = 0.0;
        self.hlRotateMenuView.alpha = 1.0;
        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.hlRotateMenuView.frame = CGRectMake(100, 0, 200, 200);
    } completion:^(BOOL finished) {
    }];
}

最後在 ViewController 中建立 HLRotateMenu,並實現 djItemMethod: 就能接收按鈕點擊的 tag。整套組件的關鍵,在於 anchorPoint 的設定、以 atan2f 求角度差,以及動畫吸附的處理。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved