Quiero rotar la imagen en sentido horario o antihorario correspondiente al toque del usuario arrastre con su velocidad. Creo que esto se puede hacer con algo de matemática y lógica. ¿Cómo sería un código de muestra para este aspecto?Girar la imagen correspondiente a la función de arrastre táctil en iPhone
9
A
Respuesta
16
Si usted está apuntando IOS 3.2 o mayor, puede utilizar UIRotationGestureRecognizer
. El código sería algo como esto:
En el método de configuración (viewDidLoad
o init
, etc,):
UIRotationGestureRecognizer *rotationGesture =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:@selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];
El controlador de eventos:
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform,
recognizer.rotation);
[recognizer setRotation:0];
}
}
Tengo algunos ejemplos más de gesto reconocedores (incluido el de arriba) en 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"];
}
Cuestiones relacionadas
- 1. UII detección de contacto táctil y arrastre
- 2. Toque y arrastre la imagen en android
- 3. Girar cara en la imagen
- 4. Imagen numpy - girar la matriz 270 grados
- 5. Girar imagen en android
- 6. Girar imagen en UIImageView
- 7. Quiero rotar la imagen en 3D de la biblioteca min3d en el evento táctil
- 8. Girar UIView iPhone
- 9. Girar una imagen en java
- 10. Girar imagen matemáticas (C#)
- 11. cómo detecto la orientación del iPhone antes de girar
- 12. Imagen de arrastre al tocar
- 13. Girar la imagen en el centro no va suave (Monodroid)
- 14. C# girar en sentido horario a la izquierda y girar a la derecha
- 15. Girar una imagen en C/C++
- 16. ¿Cómo implementar el oyente táctil en la imagen?
- 17. Girar UIImageView Dependiendo de la orientación del iPhone
- 18. Girar una imagen usando css
- 19. arrastre suavemente un Sprite en cocos2d - iPhone
- 20. UIView arrastre (imagen y texto)
- 21. Cómo evitar la escala del iPhone al girar
- 22. Zoom y arrastre una imagen en android
- 23. ¿Manera cruzada del navegador para girar la imagen usando CSS?
- 24. ¿Hay una API para la función táctil asistida de iOS?
- 25. Android Girar imagen antes de guardar
- 26. Función/complemento Eclipse que encuentra la clase junit correspondiente?
- 27. ¿Cómo convertir errno en UNIX a la cadena correspondiente?
- 28. Cómo convertir la imagen .png a .pdf en iphone programáticamente
- 29. iPhone - haciendo que la imagen quepa en la pantalla
- 30. iPhone UIButton - posición de la imagen
Esto será todo lo que u necesita – NIKHIL
cuál es ViewRotationArrow en este –
@krishna Se UIView, que gira sobre su propio centro de – NIKHIL