Move a UIImageView with an animation - iphone

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

Related

UIView main thread. Animation of 2 UIView at the same time

I`m wondering if is possible to write different UIView animation at the same time.
I trying to animate two Apple style flip counters using sprite animation simultaneously.
Each flip counter has his own animation.
The problem is that the second flip counter start to run when the first counter finish.
EDITED Twice: Here is the code: https://dl.dropbox.com/u/1348024/MultipleFlipCounters.zip
There are two simple classes “FlipCounterView.m” and “CounterViewController”.
Tapping the screen “CounterViewController” will start the animation.
Thanks!
EDITED:
Relevant code added.
Apple style flip counter sprite animation is done FlipCounterView class.
- (void)viewDidLoad{
[super viewDidLoad];
flipCounter1 = [[FlipCounterView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[flipCounter1 add:10];
[self.view addSubview:flipCounter1];
flipCounter2 = [[FlipCounterView alloc] initWithFrame:CGRectMake(0, 120, 200, 200)];
[flipCounter2 add:10];
[self.view addSubview:flipCounter2];
}
Tapping screen:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.5
animations:^{
//this method made an sprite animation on flipCounter1
[flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
[UIView animateWithDuration:0.5
animations:^{
//this method made an sprite animation on flipCounter2
[flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
Your code crashes, but give this a try after fixing the crash:
- (void)animateNow1
{
[UIView animateWithDuration:0.5
animations:^{
[flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
- (void)animateNow2
{
[UIView animateWithDuration:0.5
animations:^{
[flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self performSelector:#selector(animateNow1) withObject:nil afterDelay:0.00];
[self performSelector:#selector(animateNow2) withObject:nil afterDelay:0.00];
}
The crash in your code seems to be unrelated to the animation (Xcode 4.5.1), and if you want both animations performing simultaneously you can just do this in one animation block (since the animations attributes are the same).
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.5 animations:^{
[flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
[flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}

iPhone UIView animation until stop pressing

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

UIView Rotation

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

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