请帮助我,如何平滑地旋转(投掷)一个圆形图像时钟以及逆时针,
我尝试了很多但没有得到正确的结果。
所以请帮助我,提前谢谢。
(void)viewDidLoad { [super viewDidLoad]; ij = 0; rotatingDirection = 1; additionalOffset = 0; totalAnglesRotated = 0; isInFlingMode = TRUE; isOptionSelected = 假; viewAnimation.hidden = FALSE; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleWhateverChange)name:@“DataDownload” 对象:无];
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move :)]; [panRecognizer setMinimumNumberOfTouches:1]; [panRecognizer setMaximumNumberOfTouches:1]; [panRecognizer setDelegate:self]; [imgViewdialer addGestureRecognizer:panRecognizer];
[self Imagesswipemethod];
}
-(void)move:(id)sender {
//[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint convertedPoint = [self.view.superview convertPoint:self.view.frame.origin toView:nil];
// NSLog(@" Convert Point.. :: x = %f , y = %f",convertedPoint.x,convertedPoint.y);
[imgViewdialer bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]]; CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:view1.superview]; // NSLog(@" trans values.. :: x
= %f , y = %f",translatedPoint.x,translatedPoint.y);
int y=translatedPoint.y;
int xx = translatedPoint.x;
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x; firstY = [[sender view] center].y; }
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
if (isInFlingMode) {
//NSLog(@"Completed");
CGPoint vPoint = [panRecognizer velocityInView:imgViewdialer.superview];
double point = [panRecognizer locationInView:view1].x;
double pinty = [panRecognizer locationInView:view1].y;
NSLog(@"touch Point y %f",pinty);
NSLog(@"touch Point x %f",point);
NSLog(@"Translate Point x %d",xx);
NSLog(@"Translate Point y %d",y);
float angleToBeRotated = (vPoint.x + vPoint.y) ;
//(vPoint.x < 0) && (vPoint.y >= 0)) || ((vPoint.x < 0) && (vPoint.y < 0)
if(point>160)
{
if(y<0)
rotatingDirection = -1;
else if(y>0)
rotatingDirection = 1;
} else {
if(y<0)
rotatingDirection = 1;
else if(y>0)
rotatingDirection = -1;
}
if(pinty > 200 && xx <0)
rotatingDirection = 1;
else if(pinty >200 && xx>160)
rotatingDirection = -1;
// rotatingDirection = 1;
angleToBeRotated = (angleToBeRotated > 0) ? angleToBeRotated : (angleToBeRotated * -1);
globalAngle = angleToBeRotated;
////NSLog(@"angleToBeRotated = %f", (globalAngle / 75.0));
[NSThread detachNewThreadSelector:@selector(rotaterThreadMethod) toTarget:self withObject:nil];
} } else {
/* CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender locationInView:view1]; ////NSLog(@"Current Point X = %f, Y = %f", translatedPoint.x + firstX, translatedPoint.y + firstY); previousTouchPoint = CGPointMake(firstX, firstY);
if(!isOptionSelected) {
CGPoint center = CGPointMake(CGRectGetMidX([imgViewdialer bounds]), CGRectGetMidY([imgViewdialer bounds]));
////NSLog(@"Center Point X = %f, Y = %f", center.x, center.y);
CGPoint currentTouchPoint = CGPointMake(translatedPoint.x , translatedPoint.y);
CGFloat angleInRadians;
if((previousTouchPoint.x != 0) && (previousTouchPoint.y != 0)) {
angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
}
//previousTouchPoint = currentTouchPoint;
CGFloat angleInDegree = RadianTodegrees(angleInRadians);
angleInDegree = globalAngle+angleInDegree;
[self rotateAccordingToAngle:degreesToRadian(angleInDegree)];
revolutions+= (angleInDegree/360.0f); }*/
} }
- (void)rotateAccordingToAngle:(float) angle
{
////NSLog(@"Current Global Angle = %f", globalAngle);
totalAnglesRotated += RadianTodegrees(angle);
if(totalAnglesRotated >= 360)
totalAnglesRotated -= 360;
else if(totalAnglesRotated <= -360)
totalAnglesRotated += 360;
lblRotatingAngle.text = [[NSNumber numberWithFloat:totalAnglesRotated] stringValue];
[imgViewdialer setTransform:CGAffineTransformRotate(imgViewdialer.transform, angle)];
}
答案 0 :(得分:0)
尝试使用旋转识别器,这将对您有所帮助:
UIRotationGestureRecognizer *rotationrecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationrecognizer setDelegate:self];
[img12 addGestureRecognizer:rotationrecognizer];
之后:
-(void)rotate:(id)sender
{
NSLog(@"rotate method called");
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform =
CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
答案 1 :(得分:0)
浏览Gesture Implementation。我认为这是手势的最佳链接之一。