我想按照用戶觸摸拖動的速度順時針或逆時針方向旋轉圖像。我認爲這可以用一些數學和邏輯來完成。代碼示例是什麼樣的?旋轉對應於iPhone中的觸摸拖動的圖像
9
A
回答
16
如果您的目標是iOS 3.2或更高版本,則可以使用UIRotationGestureRecognizer
。該守則將是這個樣子:
在你的設置方法(viewDidLoad
或init
,等):
UIRotationGestureRecognizer *rotationGesture =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:@selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];
的事件處理程序:
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform,
recognizer.rotation);
[recognizer setRotation:0];
}
}
我有姿態的一些例子識別者(包括上面的那個)github。
2
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
currentTouch=touch;
if (CGRectContainsPoint([self.view frame], [touch locationInView:self.ViewRotationArrow]))
{
[self transformSpinnerwithTouches:touch];
}
else if (!CGRectContainsPoint([ViewRotationArrow frame], [touch locationInView:self.ViewRotationArrow])) {
[self dispatchTouchEvent:[touch view]WithTouch:touch];
}
}
}
-(void)transformSpinnerwithTouches:(UITouch *)touchLocation
{
CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];
//Origin is the respective point. From that I am going to measure the
//angle of the current position with respect to the previous position ....
CGPoint origin;
origin.x=240;
origin.y=160;
//NSLog(@"currentTouch Touch In Location In View:%F %F\n",touchLocationpoint.x,touchLocationpoint.y);
//NSLog(@"currentTouch Touch previous Location In View:%F %F\n",PrevioustouchLocationpoint.x,PrevioustouchLocationpoint.y);
CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
CGAffineTransform newTransform =CGAffineTransformScale(ViewRotationArrow.transform, 1, 1);
CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
CGFloat newAngle = currentRotation- previousRotation;
NSLog(@"currentRotation of x %F previousRotation %F\n",currentRotation,previousRotation);
NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
temp1=temp1+newAngle;
//NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
newTransform = CGAffineTransformRotate(newTransform, newAngle);
[self animateView:ViewRotationArrow toPosition:newTransform];
}
-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
CGPoint result;
CGFloat x = secondPoint.x-firstPoint.x;
CGFloat y = secondPoint.y-firstPoint.y;
result = CGPointMake(x, y);
return result;
}
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
//animating the rotator arrow...
[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0750];
ViewRotationArrow.transform = newTransform;
[UIView commitAnimations];
}
0
-(void)rotateView
{
//shape of eclipse
CAShapeLayer* circle = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
CGRect ViewRect = CGRectMake(self.view.bounds.size.width/2.8, (self.view.bounds.size.height-self.view.bounds.size.width/2)/2, self.view.bounds.size.width/4, self.view.bounds.size.width/2);
float midX = CGRectGetMidX(ViewRect);
float midY = CGRectGetMidY(ViewRect);
CGAffineTransform t = CGAffineTransformConcat(
CGAffineTransformConcat(
CGAffineTransformMakeTranslation(-midX, -midY),
CGAffineTransformMakeRotation(-1.57079633/0.99)),
CGAffineTransformMakeTranslation(midX, midY));
CGPathAddEllipseInRect(path, &t, ViewRect);
circle.path = path;
circle.frame = self.view.bounds;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor greenColor].CGColor;
circle.lineWidth = 3.0f;
[self.view.layer addSublayer:circle];
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 0.0f;
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[circle addAnimation:animation forKey:@"strokeEnd"];
// rotateView red color
testView=[[UIView alloc]init];
testView.frame=CGRectMake(0, 0, 20, 20);
testView.backgroundColor=[UIColor redColor];
testView.userInteractionEnabled=YES;
testView.center=CGPathGetCurrentPoint(path);
testView.transform = CGAffineTransformMakeRotation(1.57079633);
[testView sizeToFit];
[self.view.layer addSublayer:testView.layer];
// view Animation
CAKeyframeAnimation* ViewAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
ViewAnimation.duration = 15.0f;
ViewAnimation.path = path;
ViewAnimation.rotationMode = kCAAnimationRotateAuto;
ViewAnimation.calculationMode = kCAAnimationCubicPaced;
//ViewAnimation.removedOnCompletion = NO;
[testView.layer addAnimation:ViewAnimation forKey:@"position"];
}
相關問題
- 1. iPhone圖像在用戶拖動觸摸時旋轉?
- 2. 觸摸圖像的旋轉
- 3. Iphone SDK:移動並旋轉觸摸
- 4. iphone:如何旋轉帶觸摸事件的矩形圖像?
- 5. 在Android中觸摸並拖動圖像
- 6. 拖動圖像並調整它們的觸摸大小(iPhone)
- 7. 通過觸摸拖動圖像視圖
- 8. 基於觸摸事件旋轉對象
- 9. Android代碼在觸摸或拖動事件時旋轉圖像視圖
- 10. 在可可觸摸中實現基於觸摸的旋轉
- 11. 拖動UIImageView而不必觸摸圖像?
- 12. 旋轉觸摸視圖
- 13. 拖動圖像時的旋轉值
- 14. 通過觸摸並拖動它旋轉圖像直到我們想旋轉它的點
- 15. 基於觸摸問題的旋轉
- 16. 旋轉uiview使用觸摸(ipad和iphone)
- 17. 用觸摸手勢旋轉UIImageView - iphone dev
- 18. AndEngine觸摸旋轉
- 19. 想要在觸摸事件中旋轉min3d庫的3d圖像
- 20. KinectJS:通過拖放旋轉關於絕對旋轉點的圖像
- 21. iphone形象。多點觸摸/拖動
- 22. 如何將縮放,拖動和旋轉應用於Android中的圖像
- 23. 當在對象本身上觸摸時拖動圖像
- 24. 可拖動圖像的jQuery UI圖像旋轉木馬
- 25. 圖像沒有指向(旋轉)你觸摸的地方
- 26. 如何降低UIView觸摸保持旋轉圖像的速度?
- 27. 根據用戶在屏幕上的觸摸旋轉圖像
- 28. 基於觸摸的iPhone動畫
- 29. 拖動基於觸摸的攝像頭(Unity)
- 30. Unity5 C#:如何通過觸摸拖動來旋轉游戲對象
這將是u需要 – NIKHIL
什麼ViewRotationArrow在此 –
@krishna它的UIView這是它自己的中心 – NIKHIL