Iphone SDK: move and rotate to touch - iphone

I have an image of a fish - If the user touches the screen I want the fish to "look" at the touched point and move there. If the touch has been moved i want the fish constantly following the touch.
How can I do that?

would be something like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *tap = [touches anyObject];
CGPoint pointToMove = [tap locationInView:self.view];
CGFloat xdiff = pointToMove.x-myFish.center.x;
CGFloat ydiff = pointToMove.y-myFish.center.y;
if (!CGRectContainsPoint(myFish.frame, pointToMove)) {
CGFloat angle = 0;
if (xdiff) {
if (xdiff>0) {
angle = atanf(ydiff/xdiff);
} else {
angle = M_PI + atanf(ydiff/xdiff);
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myFish.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations];
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myFish.center = pointToMove;
[UIView commitAnimations];
}
and you probably want to implement these too:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
EDIT: updated code, now the rotation is done in a separate animation, so that the fish rotates faster than he swims.

Related

Animate a UIImageView in the middle of an animation

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.

Slide textview horizontal without using paging

I`m using a simple Textview in an tabbased application to display some text :) I figerd out how to change the text animated using the following code:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self];
// To be a swipe, direction of touch must be horizontal and long enough.
if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN &&
fabsf(startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX)
{
// It appears to be a swipe.
if (startTouchPosition.x < currentTouchPosition.x){
NSString *string = [[NSString alloc]initWithFormat:#"..."];
self.text = string;
}
else{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self superview] cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[self superview] cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
[self displayNewText];
}
}
However there is now "slide transition" defined in UIViewAnimationTransition. I know about paging, but I don`t want to use this. Is there an other possibility to have an horizontal slide animation when detecting a horizontal swipe?
I think you are almost there. Intercept touchesMoved:withEvent method just like in your current code and update the visible area of the UIScrollView object.
I hope you don't have to detect touches for your requirement use UIScrollView as parent view for UITextView.
scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0,40, 320,200)];
[scroll setContentSize:CGSizeMake(640, 200)];
Or if you want to animate the view you can use affine transforms.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
someview.transform=CGAffineTransformMakeTranslation(-100, 0);
[UIView commitAnimations];

how move an image on mainview

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

View rotation in touchesMoved

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];
}

change counterclockwise to clockwise with CGAffineTransformIdentity

I have a telephone wheel. On the touchend, it goes to his position with an animation.
Until the angle is less than 180°, it returns clockwise. No problem, with this code :
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.5];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
But It goes wrong after that and continue to rotate for a complet turn.
I tried to make to animations like this :
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
wheel.transform = CGAffineTransformRotate(wheel.transform, degreesToRadians(-130));
[UIView commitAnimations];
[self performSelector:#selector(animatewheelViewToCenter) withObject:nil afterDelay:0.3];
}
- (void)animatewheelViewToCenter{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.3];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
It works, but the animation isn't fluid ; the change is visible.
I'm not sure what the state is when touchesEnded (in terms of rotation) and I'm assuing you chose degreesToRadians(-130) to try and do it partially and expect the next method to do the rest. This should be a better solution that hopefully yields the result you're expecting. I'm not sure what cadran is or why you're rotating that, so I'll just rotate the wheel.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
wheel.transform = CGAffineTransformMakeRotation(degreesToRadians(-130));
// I would recommend that the degrees you rotate be half of whatever your rotation is.
// this would make the wheel rotate to the home position more evenly
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animatewheelViewToCenter:finished:context:)];
[UIView commitAnimations];
}
- (void)animatewheelViewToCenter:(NSString *)animationID finished:(NSNumber *)finished context:(id)context {
[UIView setAnimationBeginsFromCurrentState:YES]; // you might need to make this NO
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.2];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
EDIT: Actually, I would probably make the rotation (in the example -130 degrees) slightly more than what half would be, because CGAffineTransformIdentity is gonna take the shortest path to go back to regular, so if you go exactly 1/2 way, it may not go the correct direction (clockwise or counter-clockwise) that you want.