gpt4 book ai didi

ios - 为什么我的可旋转轮控件有时会向错误的方向旋转?

转载 作者:行者123 更新时间:2023-11-29 12:39:39 25 4
gpt4 key购买 nike

我正在制作一个可旋转的滚轮控件。您可以在其上的任何位置拖动以通过其中心点旋转控件。

它的行为不正确。它通常运行良好,但对于旋转的某些部分,它会开始向相反方向旋转一小段时间,然后再次开始用您的手指旋转。 编辑:经过进一步测试,我意识到您无需将其旋转 360 度,它就会朝错误的方向旋转。

一个可能不相关的问题是控制似乎比你的手指移动得慢。如果原始问题的解决方案不能解决这个问题,我会提出一个单独的问题。

我发布了一个 example project .如果您更愿意自己重现该项目,以下是带代码的步骤:

  1. 创建一个新的单 View 应用程序。
  2. 创建一个名为 NNReadyIndi​​cator 的新 UIView 子类(在 Objective-C 中)
  3. 将此代码复制并粘贴到`NNReadyIndi​​cator.m

#import "NNReadyIndi​​cator.h"

@interface NNReadyIndicator ()

@property CGAffineTransform startTransform;

@property double originalAngle;

@end

@implementation NNReadyIndicator

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/

#pragma mark tracking

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchPoint = [touch locationInView:self];

[self setOriginalAngle:[NNReadyIndicator calculateAngleWithX1:touchPoint.x x2:self.center.x y1:touchPoint.y y2:self.center.y]];
[self setStartTransform:self.transform];

NSLog(@"BEGIN TRACKING | originalAngle: %f", self.originalAngle);

return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchPoint = [touch locationInView:self];
double angle = [NNReadyIndicator calculateAngleWithX1:touchPoint.x x2:self.center.x y1:touchPoint.y y2:self.center.y];
CGFloat angleDifference = (CGFloat)(self.originalAngle - angle);

[self setTransform: CGAffineTransformRotate(self.startTransform, -angleDifference)];

NSLog(@"CONTINUE TRACKING | angle: %f angleDifference: %f", angle, angleDifference);

return YES;
}

#pragma mark -- Calculate Angle

+ (double)calculateAngleWithX1:(double)x1 x2:(double)x2 y1:(double)y1 y2:(double)y2 {
double distanceX = [NNReadyIndicator calculateDistanceWithPoint:x1 point:x2];
double distanceY = [NNReadyIndicator calculateDistanceWithPoint:y1 point:y2];

return [self calculateAngleWithDistanceX:distanceX distanceY:distanceY];
}

+ (double)calculateDistanceWithPoint:(double)pointA point:(double)pointB {
double distance = pointA - pointB;

if (distance < 0) {
distance = -distance;
}

return distance;
}

+ (double)calculateAngleWithDistanceX:(double)distanceX distanceY:(double)distanceY {
return atan2(distanceY, distanceX);
}

@end

编辑:添加了日志记录

为什么控件不会一直以与拖动它的手指相同的方向(并以相同的速度?)旋转?

最佳答案

如果高于或低于 360 度,则需要翻转角度。此外,为了更容易理解,您必须将其转换为度数,然后再转换回来。基于这个答案:https://stackoverflow.com/a/19617316/2564682

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchPoint = [touch locationInView:self];
double angle = [NNReadyIndicator calculateAngleWithX1:touchPoint.x x2:self.center.x y1:touchPoint.y y2:self.center.y];

double angleInDegrees = angle * 180 / M_PI;

// Flip angle if above or below 360 degrees
if (angleInDegrees > 180) {
angleInDegrees -= 360;
} else if (angleInDegrees < -180) {
angleInDegrees += 360;
}

CGFloat angleDifference = (CGFloat)(self.originalAngle - angleInDegrees);

[self.layer setTransform: CATransform3DRotate(self.layer.transform, (float)DEGREES_TO_RADIANS(-angleDifference), .0, .0, 1.0)];

NSLog(@"CONTINUE TRACKING | angle: %f angleDifference: %f", angleInRadians, angleDifference);

return YES;
}

关于ios - 为什么我的可旋转轮控件有时会向错误的方向旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com