iPhone UIView animation until stop pressing - iphone

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

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

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

Animate resize of view during rotation without autoresize

If I set the AutoResize mask of my view, then they will resize together with the main window during device rotation.
Is there a way to get the same smooth performance by using my own animation and no autoresize?
Yes, you can certainly use UIView animations to move elements around.
Also, a common technique is to hide the interface as part of
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
redraw it and show it as part of
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Here is an example:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myTable.hidden = YES;
...
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self redrawInterface];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myTable.hidden = NO;
...
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}

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.