scrollViewWillEndDraggin:WithVelocity:targetContentOffset delegate method bug - ios5

I am implementing a UIScrollView and its delegate in a UIViewController. It scrolls in the horiztonal way.
WHat I want is to set "magnetism" when the view is dragged and is stabilizing.
to do that, I am listening to the delegate, specially the method mentionned in titled.
It returns me the offset of the final destination.
then I make the scollview display the view which correspond to this destionation with setContentOffset or setvisiblerect methods.
I also NSlog the entire method to catch the bugs.specially right after calling the method I have a nslog which confirmed me/or not, if the method is triggered.
problem : when I build&run, the first nslog is triggered an average of 3 times by dragging.
so the entire effect looks very strange and I can't figure out whether or not it works.
I have found very little things about this on the net. So it would be a great help if you had some clues.
cheers
I am kind of desperate :/

So you want the scroll view to snap to a valid selection when the user is done dragging? You should be able to implement this using the
- (void)scrollViewWillBeginDragging:(UIScrollView *)thisScrollView
and
- (void)scrollViewWillDidScroll:(UIScrollView *)thisScrollView
methods. Without seeing your code I can't tell you exactly how to go about this, but I would recommend keeping track of the thisScrollView.contentOffset.x in a variable, and doing some arithmetic to move the scrollView with a
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
call to move it to the appropriate spot.

Related

Detect "end decelerating" in a programmatically moved scrollview

I had a manually dragged scrollview, now I want a scrollview moved programmatically when I click a button, by this code:
offset = CGPointMake(scrollView.contentOffset.x+320, 0);
[UIScrollView beginAnimations:#"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:0.5];
[scrollView1 setContentOffset:offset];
[UIScrollView commitAnimations];
It works well but now the functions scrollViewBeginDragging and scrollViewEndDecelerating are not called. Begindragging is no problem for me, but I don't know how detect the end of de scrollview movement when it is fired programmatically.
Thanks in advance!
Implement the scrollViewDidEndScrollingAnimation: delegate method. That's exactly what it's for.
Well... you kind of know when that's supposed to get called, right? Those callbacks are intended for touches, so they won't work, but you're telling it to finish 0.5, so why not schedule a call to the delegate method yourself?
[self performSelector:#selector(scrollViewDidEndDecelerating:) withObject:scrollView afterDelay:0.5];

Animation is effecting every view even after calling commitAnimation

I am developing a Cocos2D game. On a stage a show a UIView giving users a list of tasks to perform. There are text fields which I move up or down using Animation. My code is below.
[textField setFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.size.width, textField.frame.size.height)];
[inviteButton setFrame:CGRectMake(inviteButton.frame.origin.x, inviteButton.frame.origin.y, inviteButton.frame.size.width, inviteButton.frame.size.height)];
[headingLabel setFrame:CGRectMake(headingLabel.frame.origin.x, headingLabel.frame.origin.y, headingLabel.frame.size.width, headingLabel.frame.size.height)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[textField setFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y-50, textField.frame.size.width, textField.frame.size.height)];
[inviteButton setFrame:CGRectMake(inviteButton.frame.origin.x, inviteButton.frame.origin.y-50, inviteButton.frame.size.width, inviteButton.frame.size.height)];
[headingLabel setFrame:CGRectMake(headingLabel.frame.origin.x, headingLabel.frame.origin.y-30, headingLabel.frame.size.width, headingLabel.frame.size.height)];
[UIView commitAnimations];
[self.view.layer removeAllAnimations];
Problem is that even after user is done and he presses the cancel button to remove this view from the super view, every view including UIAlertView and other subviews appearing in my map after removing that view are effected by the animation i used to move text fields and label up or down. UIAlertView is using same Animation to show up and so is happening other subviews. Can anyone please tell me why this is happening?
Best Regards
You have to set the animationID to a unique value in the beginAnimations:context: method.
See the official doc
Also this doc states:
Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.

Typing while animation UITextView

I'm trying to have a variable-height UITextView which changes size to accomodate its contents, but when the frame changes in the size-change animation, one or two keystrokes aren't captured in the UITextView. The animation duration is 0.1s, and typically it only misses one letter when you're typing fairly fast. It is, however, very consistent in missing letters when the animation happens. The following animation block occurs within the textViewDidChange: delegate message:
[UIView animateWithDuration:0.1 animations:^{
[textView setFrame:CGRectMake(...)];
}];
I've Googled and searched on SO, but so far nothing has come up. Has anyone else encountered this?
By default UIView animations disable user interaction for animated view, so possibly (sorry cannot test it now) explicitly enabling user interaction will solve your problem:
[UIView animateWithDuration:0.1
delay:0.0
options: UIViewAnimationOptionAllowUserInteraction
animations:^{
[textView setFrame:CGRectMake(...)];
}
completion:^(void){}];

Source of UIView Implicit Animation delay?

I have a block of UIView animation code that looks like this:
[UIView beginAnimations:#"pushView" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:#selector(animationWillStart)];
view.frame = CGRectMake(0, 0, 320, 416);
[UIView commitAnimations];
The code basically mimics the animation of a ModalView presentation and is tied to a button on my interface. When the button is pressed, I get a long (.5 sec) delay (on iPod Touch...twice as fast on iPhone 3GS) before the animationWillStart: actually gets called. My app has lots going on besides this, but I've timed various points of my code and the delay definitely occurs at this block. In other words, a timestamp immediately before this code block and a timestamp when animationWillStart: gets called shows a .5 sec difference.
I'm not too experienced with Core Animation and I'm just trying to figure out what the cause of the delay is...Memory use is stable when the animation starts and CoreAnimation FPS seems to be fine in Instruments. The view that gets animated does have upwards of 20 total subviews, but if that were the issue wouldn't it cause choppiness after the animation starts, rather than before? Any ideas?
Try it with a single subview or with no subviews at all to make sure the delay is not caused by so many children.
Profile the code in Instruments to see where exactly the code lags. You might get down to some internal Core Animation function call that will hint you what’s going on.
Try the code without the “lot that’s going on” to make sure you’re not stepping on Core Animation’s toes with your other code.
Or, in short: experiment and measure, because conjectures seldom work when optimizing.
In your pasted block, you specify the selector animationWillStart (no colon), but later in your question, you refer to animationWillStart: (with colon). These selectors are not equivalent, so is it possible that your intended selector is never being called on account of this animation, and is being called 0.5 seconds later on account of some other animation?

Iphone SDK - Animating Subview

I have a View that has a UIWebView, and an OptionsPane (Custom UIViewController with Custom view).
I want when the view is shown, for the options pane (located on the top of the main view) to FLIP into place. I am using the code, and I am getting a strange result.
The FIRST time the view is shown, the options pane seems to already be visible... When I hit BACK on my navController, and pull up the View again, the animation works perfectly.
Can anyone shed some light on this topic?
- (void)viewDidLoad {
[super viewDidLoad];
optionsPane=[[OptionsPaneController alloc] initWithNibName:#"OptionsPane" bundle:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[optionsPane.view removeFromSuperview];
[self checkOptionsVisible];
}
-(void)checkOptionsVisible{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[optionsPane view] cache:YES];
[[self view] addSubview:[optionsPane view]];
[theWebView setFrame:CGRectMake(0,87,320,230)];
[[optionsPane view] setFrame:CGRectMake(0,0,320,87)];
[UIView commitAnimations];
}
Hmm, I don't think the viewWillAppear message is getting sent the first time. There are two things that I read in the SDK. You should call super inside that message and there is a big warning that may apply to your first time:
Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.
Ultimately, I would run through the debugger and make sure that viewWillAppear message is being sent when you think it is.
If I understand what your explaining, I had a very similar problem the other day.
What happening on the first load is that viewDidLoad fires first. loading the nib file takes a bit more time than it takes for the viewWillAppear to fire itself.
What we're getting is a nib loads after the viewWillApper already retired.
On any load after that, the viewDidLoad will not fire, letting the viewWillAppear to do its loyal flipping job.
What to do?
First, try to change your code to use "viewDidAppear". That should help, but you have to see if it looks good.
Another option (ugly one, I know) is to have a call to checkOptionsVisible on the viewDidLoad too.
If non of that help, I would consider a timer as a hack - if the requirements allow it.
I hope that make you closer to solve the problem.
Updated for your situation:
Instead have four views:
A backing view
The main View
The back view (options pane) 100 pixels
The front view (blank view) 100 pixels
Add the main view to the backing view as normal.
Add the front view to the backing view where you would like the options pane to appear.
make sure the front and back view have the same frame.
Use the same code as below using the methods flip the front and back views.
Original Answer
You need 3 views:
A backing view
The front View
The back view
The backing view just holds the other 2 as they flip back and forth Below are the flipping methods. I place them both in backingViewController:
- (void)displayBack{
//parent controller is an ivar so the sub-view controllers know who their daddy is
backController.parentController = self;
[UIView beginAnimations:nil context:#"flipTransitionToBack"];
[UIView setAnimationDuration:1.2];
//note self.view IS the backing view
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//remove the front view
[[frontController view] removeFromSuperview];
//add the back view view
[self.view addSubview:[backController view]];
[UIView commitAnimations];
//giving a heads up to the view that is about to come on screen
[backController viewWillAppear:YES];
}
- (void)displayFront{
[UIView beginAnimations:nil context:#"flipTransitionToFront"];
[UIView setAnimationDuration:1.2];
[UIView setAnimationDelegate:self];
//I'm interested in knowing this has happened
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidEndWithID:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//remove back view
[[backController view] removeFromSuperview];
//add the front view
[self.view addSubview:[frontController view]];
[UIView commitAnimations];
}
The view property on a UIViewController is lazily-loaded -- even when you're init-ing with a nib as you are here, the view itself doesn't actually get instantiated until the first time the property is accessed.
It's hard to know exactly what's happening without seeing more code, but you may get the results you want if you access optionsPane.view in viewDidLoad (you don't need to do anything with it, just access the property to force loading).