Chcę obrócić obraz w kierunku zgodnym z ruchem wskazówek zegara lub przeciwnym do ruchu wskazówek zegara, odpowiadającego przeciągnięciu dotykiem użytkownika z jego prędkością. Myślę, że można to zrobić za pomocą pewnej matematyki i logiki. Jak wyglądałaby przykładowy kod?Obracanie obrazu odpowiadającego przeciągnięciu dotykiem w iPhonie
9
A
Odpowiedz
16
Jeśli kierujesz iOS 3.2 lub nowszego, można korzystać UIRotationGestureRecognizer
. Kod byłoby coś jak to wygląda:
W swojej metodzie setup (viewDidLoad
lub init
, etc,):
UIRotationGestureRecognizer *rotationGesture =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:@selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];
obsługi zdarzenia:
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform,
recognizer.rotation);
[recognizer setRotation:0];
}
}
Mam jeszcze kilka przykładów gest identyfikatory (w tym powyższy) na 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"];
}
Powiązane problemy
- 1. Obracanie obrazu w UIImageView
- 2. Obracanie obrazu w Matplotlib
- 3. Obracanie obrazu w Androidzie
- 4. Obracanie obrazu w C/C++
- 5. Złe obracanie obrazu
- 6. Obracanie obrazu za pomocą css
- 7. Obracanie obrazu wokół osi Y
- 8. obracanie obrazu za pomocą css
- 9. Obracanie obrazu bez obkurczania w systemie Android
- 10. Obracanie obrazu przestawnego w Centrum - Android
- 11. Łączone obracanie obrazu i przycinanie w jQuery
- 12. Uzyskiwanie obrazu z ramki wideo w iPhonie
- 13. Obracanie obrazu tła za pomocą CSS3
- 14. Jak uzyskać płynne obracanie obrazu w systemie Android?
- 15. Obracanie obrazu w centrum nie działa gładko (Monodroid)
- 16. Zatrzymaj i rozpocznij animację dotykiem. Cel C
- 17. Zrozumienie logiczny system pierwszego odpowiadającego
- 18. Jak zmienić rozmiar obrazu z dobrą jakością w iPhonie?
- 19. CSS: obracanie obrazu i wyrównanie u góry po lewej
- 20. Obracanie obrazu PHP traci jakość po każdym obrocie
- 21. Obracanie obrazu przez jego własne centrum na płótnie
- 22. Obracanie i przycinanie UIImage
- 23. Jak wykryć "poruszenie" po przeciągnięciu?
- 24. Anuluj UIScrollView bounce po przeciągnięciu
- 25. Obracanie animacji Android
- 26. Obracanie UIImage w szybkim
- 27. UIImage Zapisywanie obrazu z nazwą pliku na iPhonie
- 28. Obracanie UIAlertView
- 29. Obracanie punktu o inny punkt w 2D
- 30. Widok alertu w iPhonie
To będzie wszystko, co u potrzeba – NIKHIL
Siema ViewRotationArrow w ten –
@krishna Jest UIView, który obraca się na jego własnym centrum – NIKHIL