Hello All I want to rotate UIView on single finger touch and it still rotate untill finger moves on screen of iPhone and it stops rotation when I stop the finger moving or remove it from screen.
Thanks in Advance.
Try similar code
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
CGAffineTransform rr=CGAffineTransformMakeRotation(5);
yourView.transform=CGAffineTransformConcat(yourView.transform, rr);
[UIView commitAnimations];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
Pradeepa's code is nice, however, that animation system is getting deprecated (if not already). Try something like this instead:
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
double startRotationValue = [[[yourView.layer presentationLayer] valueForKeyPath:#"transform.rotation.z"] doubleValue];
rotation.fromValue = [NSNumber numberWithDouble:startRotationValue];
rotation.toValue = [NSNumber numberWithDouble:startRotationValue+5];
rotation.duration = 0.2;
[yourView.layer addAnimation:rotation forKey:#"rotating"];
I took Pradeepa's numbers to make them comparable, but i believe Apple prefers you using this or block-based animation instead of the old system.
rotating a view on touch may help you
Related
I have an object that is moving right to left across the screen:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.8];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myImageview.layer.position = CGPointMake(20, myImageView.layer.position.y);
[UIView commitAnimations];
I found out that even while the animation is still happening, XCode already marks the image's location as its final destination, and in order to detect a touch on the moving image, I need to use the presentationLayer:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([myImageview.layer.presentationLayer hitTest:touchPoint]) {
NSLog(#"it's a hit!");
}
}
This part works.
Now, I'd like the image to move up when it is pressed.
I want the image to move up while it continues it's sideways movement.
Instead, this code moves the image not just up, but also all the way to its final destination on the left:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
NSLog(#"it's a hit!");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myImageView.layer.position = CGPointMake( mouse.layer.position.x, myImageView.layer.position.y - 40);
[UIView commitAnimations];
}
}
I want the image to move up while it continues it's sideways movement.
Does anyone know of a way to accomplish this?
Thanks so much!
Have you tried setting the animation option UIViewAnimationOptionBeginFromCurrentState?
(I say option because it is an option in the block-based animation methods introduced with iOS 4. It is also available as [UIView setAnimationBeginsFromCurrentState:YES] if you can't switch away from the deprecated UIView class methods just yet.)
touchesBegan becomes (using block animations):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
NSLog(#"it's a hit!");
[UIView animateWithDuration:0.5 delay:0.0 options:(UIViewAnimationOptionCurveLinear & UIViewAnimationOptionBeginFromCurrentState) animations:^{
myImageView.layer.position = CGPointMake( myImageView.layer.position.x, mouse.layer.position.y - 40);
}completion:^(BOOL complete){
//
}];
}
}
If you use that you should be able to specify the final x and y coordinates you want and have animation proceed from the point at which the object was touched to that position.
i have two views and one viewcontroller: one contains UIImageView with image, second have touchesBegan and touchesEnded method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[viewcontroller moveMethod];
}
How to make moveMethod method to make the animation (eg. moving the x-axis) repeat smoothly until I stop to push the view?
try this code to move your view on x-axis
CGRect frame=view.frame;
frame.origin.x=frame.origin.x+200;
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
view.frame=frame;
[UIView commitAnimations];
i've got a problem with iPhone SDK.
I want create an imageView that user can touch and move and scale as he wish.
How can i do this?
Thank you
the easy way is to put it in a UIScrollView and enable the methods of the UIScrollViewDelegate that you wan't to use (scrool / zoom)
take a look at the apple sample project "PhotoScroller", you can find it in xCode help...
ciao,
luca
Look at UIGestureRecognizer : http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
Especially, UIPanGestureRecognizer and UIPinchGestureRecognizer
for moving an object, you can CGAfflineTransformation to move from x axis to y axis.
u write up those code in Touch Delegates. also set the UIImage frame as CGRectZero.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.50];
titl.transform=CGAffineTransformMakeTranslation(260, 0);
comment.transform=CGAffineTransformMakeTranslation(0, 100);
[UIView commitAnimations];
this is the code to move an object from one place to another. this might help you
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView beginAnimations:nil context:NULL];
CGPoint touchPoint = [[touches anyObject] locationInView:[self window]];
float deltaX = touchPoint.x - lastUpdatedPoint.x;
float deltaY = touchPoint.y - lastUpdatedPoint.y;
float newCenterX = [[self layer] position].x + deltaX; float newCenterY = [[self layer] position].y + deltaY;
CGPoint center = CGPointMake(newCenterX, newCenterY);
[[self layer] setPosition:center];
lastUpdatedPoint = touchPoint;
[UIView commitAnimations];
}
BY
CNSivakumar
I rotate a view by 45 degree on every dragging. Here is my code. Problem here is that the view is rotated only once. After that there is no rotation. What will be the reason? Any help
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0];
dialView.transform = CGAffineTransformMakeRotation(45* M_PI/180);
[UIView commitAnimations];
}
You can also use CGAffineTransformRotate to rotate from its present transform, as in:
dialView.transform = CGAffineTransformRotate(dialView.transform, 45 * M_PI / 180);
The rotation is always relative to 0. If you rotate to x several times it will rotate only the first time.
If you want to rotate 45 everytime you call that code, you will need to have counter and it would look like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
step++;//You are supposed to keep this variable global.
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0];
dialView.transform = CGAffineTransformMakeRotation(45*step* M_PI/180);
[UIView commitAnimations];
}
I have implemented a slider bar type UI control, like the Unlock mechanism on the iPhone.
When the touchesEnded method gets called, I return the slider to it's starting position. Instead, I would like to animate its move back to it's starting position.
The code should be something like this, but it doesn't work. What am I doing wrong here?
Self.unlock is a UIImageView:
CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:#"position.x"];
theAnimation.fromValue = [NSNumber numberWithFloat:BEGINX];
theAnimation.toValue = [NSNumber numberWithFloat: 0.0];
theAnimation.duration = 0.5;
[self.unlock addAnimation: theAnimation];
Sorry for the bone-headed question!
You can probably do this in a much simpler way using implicit animation. Assuming you currently are doing something like this:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
...
myView.frame = startingFrame;
...
}
You can use the implicit animator to animate any changes you make to the view (such as its frame):
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
...
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
myView.frame = startingFrame;
[UIView commitAnimations];
...
}