gpt4 book ai didi

ios - UIImageView 触摸方向旋转动画

转载 作者:行者123 更新时间:2023-11-28 18:06:59 30 4
gpt4 key购买 nike

我有一个带有箭头图像的 UIImageView。当用户点击 UIView 时,这个箭头应该指向点击的方向,保持它的位置,它应该只是改变变换。我已经实现了以下代码。但它没有按预期工作。我添加了一个屏幕截图。在此屏幕截图中,当我触摸左上角的点时,箭头方向应如图所示。但事实并非如此。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch=[[event allTouches]anyObject];
touchedPoint= [touch locationInView:touch.view];
imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11));
previousTouchedPoint = touchedPoint ;
}

- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{

CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}

enter image description here

最佳答案

我假设你想要一个箭头图像指向你触摸的地方,我试过了,这就是我能想到的。我放置了一个带有向上箭头的 ImageView (没有尝试从任何其他位置开始,日志给出了正确的角度)并且在触摸不同位置时它旋转并指向触摸位置。希望它有所帮助(尝试了一些旧数学:-))

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch=[[event allTouches]anyObject];
touchedPoint= [touch locationInView:touch.view];
CGFloat angle = [self getAngle:touchedPoint];
imageView.transform = CGAffineTransformMakeRotation(angle);

}

-(CGFloat) getAngle: (CGPoint) touchedPoints
{
CGFloat x1 = imageView.center.x;
CGFloat y1 = imageView.center.y;

CGFloat x2 = touchedPoints.x;
CGFloat y2 = touchedPoints.y;

CGFloat x3 = x1;
CGFloat y3 = y2;

CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));

CGFloat angle = atanf(oppSide/adjSide);
// Quadrant Identifiaction
if(x2 < imageView.center.x)
{
angle = 0-angle;
}


if(y2 > imageView.center.y)
{
angle = M_PI/2 + (M_PI/2 -angle);
}
NSLog(@"Angle is %2f",angle*180/M_PI);
return angle;

}

-anoop4real

关于ios - UIImageView 触摸方向旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11431471/

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