大家無論在玩王者榮耀,還是絕地求生等升級版的游戲時,都會遇到游戲操作控制盤。
基于此,個人開發了一個簡易的操作盤,主要包括兩個部分:操作控制臺背景 和 中間操作圓球。
基于此,定義:consoleBGImageView,放置一張初始Image;定義中間操作圓球為:rotateButton
通過約束條件,來控制rotateButton只能在consoleBGImageView范圍內轉動
通過UIPanGestureRecognizer添加rotateButton拖動手勢,當轉動到不同的方向時,修改相應的背景圖,這樣可以知道轉動到哪個方向。
通過如下代碼,控制手指觸碰的點限制在圓盤內部
-(void)rotateLocation:(CGPoint)pLocation {
if([self IfYuntaiViewIsIntheBigView:pLocation]){
self.rotateButton.center = CGPointMake(pLocation.x, pLocation.y);
CGPoint tranPoint = CGPointMake(pLocation.x - centerViewX, pLocation.y - centerViewY);
ELPCircleConsoleMoveDirection direction = [self determineCameraDirection:tranPoint];
currentDirection = direction;
} else {
CGFloat radius = self.traceView.size.width * 0.5;
CGPoint pointc = self.consoleBGImageView.center;
CGPoint changePoint = [self CirclePoint:radius withCenterCircle:pointc withCurrentPoint:pLocation];
self.rotateButton.center = CGPointMake(changePoint.x, changePoint.y);
CGPoint tranPoint = CGPointMake(changePoint.x - centerViewX, changePoint.y - centerViewY);
ELPCircleConsoleMoveDirection direction = [self determineCameraDirection:tranPoint];
currentDirection = direction;
}
CGFloat xValue = fabs(self.rotateButton.centerX - centerViewX);
CGFloat yValue = fabs(self.rotateButton.centerY - centerViewY);
CGFloat currentRadiusValue = xValue * xValue + yValue * yValue;
CGFloat bgImgRadiusWith = self.consoleBGImageView.width * 0.5 * 0.5;
if(currentRadiusValue > bgImgRadiusWith * bgImgRadiusWith * 0.6) {
[self sendRotateButtonRotateCmd];
} else {
[self resumBackgroundImgToNormal];
}
}
詳細代碼詳見如下鏈接,如果對你有幫助,麻煩star一個,謝謝。
https://github.com/ElsonPeng/ELPCircleConsoleView
效果圖如下:
screenshot.png
Simulator Screen Shot - iPhone 8 Plus - 2018-03-04 at 22.43.55.png