How to have UIScrollView animate off screen when dragged - ios

So I have a UIViewController and I have a UIScrollView inside of it, which has some UI elements.
I want the user to be able to drag the UIScrollView up and down, but when it drags past a certain threshold, I want it to animate off the screen. There needs to be some springiness to it though. I tried using -(void)scrollViewDidScroll: but couldn't seem to get it right. Any thoughts?

In scroll view delegate method -(void)scrollViewDidScroll:(UIScrollView *)scrollView; observe scrollView.contentOffset.y and trigger animation when it reaches expected y position.
for example:
CGFloat yTranslation = -200.f; // Value you want to shift
[UIView animateWithDuration:0.35 animations:^{
scrollView.transform = CGAffineTransformMakeTranslation(0, yTranslation);
} completion:^(BOOL finished) {
// code after completion
}];

Related

Sliding a large image within UIImageView

I have a large image with 1000 px width. I want to display the image in original size and I want it to automatically scroll from right to left in an animation. For example in IPhone 6 it should display the 375 px first, then should move to the left till the all parts of photo is displayed in time. How can I do this?
// You have to put the imageView inside a UIScrollView
// Set the duration you want for your animation
[UIView animateWithDuration:1.5f animations:^{
//Code for animation
[scrollView setContentOffset:CGPointMake(imageView.frame.size.width - scrollView.frame.size.width, scrollView.contentOffset.y) animated:NO];
} completion:^(BOOL finished) {
//Code after the animation is completed
}];
You can programmatically scroll UIScrollView with setContentOffset function. For example:
scrollView.setContentOffset(CGPoint(x: scrollView.frame.width - imageView.frame.width, y: 0), animated: true)
You can also set the contentOffset property within UIView.animateWithDuration block for more controlled animation.

How keep uicollectionview scroll at a direction without drag?

I want uicollectionviewcell move like things on assembly line.
Now I use
(void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
in a method and in the last of method i make method call it self.
But UICollectionViewCell move and stop and go on.
UICollectionView inherits from UIScrollView, so you can use
// determine point, which you want to scroll
CGPoint scrollPoint = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
// scroll to it, with custom animation duration
[UIView animateWithDuration:5 animations:^{
[self.tableView setContentOffset:scrollPoint];
// Also you can use scrollToItemAtIndexPath here instead of setContentOffset
}];

animate slide up and down UIView based on indexPath.row

right now I'm trying to move up and move down an UIView based on roll up or down in UITableView. So, if I rolled up the view, which means indexPath.row will keep incremented; the view will appear slide down until certain position and stopped. Then when I rolled down the table view, which means indexPath.row, decremented; the view will disappear by slide up outside the display view.
Do anyone have any idea what I need to code? Right now I'm just simply able to hide and unhide the view. I need the code for animate it slide up and down.
mark=indexPath.row;
flag=flag+1;
if (flag-mark>1) {
postBox.hidden=NO;
flag=indexPath.row;
}
else
postBox.hidden=YES;
Thanks in advance for your help ^^
If you want to move the view according to the movement of the UITableView, you can calculate the content offset of the UITableView and apply it to your moving view. Declare a ivar of type CGFloat named lastOffset.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat distance = scrollView.contentOffset.y - lastOffset;
lastOffset = scrollView.contentOffset.y;
[UIView animateWithDuration:0.2 animations:^{
self.movingView.frame = CGRectOffset(self.movingView.frame, 0.0f, distance);
}];
}

How to scroll back up with a uiscrollview?

Basically I have a uiscrollview. The uiscrollview scrolls down when i push a certain button. However once the content offset has been set it will not allow me to scroll the uiscrollview back to the to.
For example...
If I set the content off set to 100 and animated:yes. The scrollview will scroll to that position and allow me to continue to scroll down the page but when trying to scroll up you pull up and see the upper par of the view but it then bounces back down.
Here is my code:
Firstly when the button is pressed I call the void function:
[self downExecute:scroller];
Secondly I run the void statement which set's my content off set...
- (void)downExecute:(id)sender {
NSLog(#"scroll down");
CGFloat currentOffset = scroller.contentOffset.y;
CGFloat newOffset;
newOffset = currentOffset + 100;
[UIScrollView animateWithDuration:0.3 animations:^(void) {
[scroller setContentOffset:CGPointMake(0.0,newOffset)];
}completion:^(BOOL finished) {
}];
}
Any Help would be greatly appreciated...
If your intent is to allow normal scrolling after you've repositioned the content, you could use scrollRectToVisible:animated: instead of changing the content offset.
try something like this to allow scrolling:
scroller.alwaysBounceVertical = YES;
and to manage duration of scrolling use this:
scroller.viewForBaselineLayout.layer.speed = 1.0f; // 1.0f is default
[scroller setContentOffset:(some CGPoint) animated:YES];

UIScrollView's content offset being reset

I have been struggling for a long time with a problem with a UIScrollView.
I have a UIScrollVIew that contains a UITextView as a subview. When I select the text view a keyboard pops up. I want to resize the text view to fit exactly in the available space, and also scroll the scroll view so that the text view is positioned exactly in the visible space (not hidden by the keyboard).
When the keyboard appears I call a method that calculates the appropriate size for the text view and then performs the following code:
[UIView animateWithDuration:0.3
animations:^
{
self.textView.frame = frame;
}
];
[self.scrollView setContentOffset:CGPointMake(0,frame.origin.y) animated:YES];
(here frame is the appropriate frame for the text view).
Unfortunately the scroll view does not always scroll to the correct position, especially when it is already at a non zero vertical content offset when I select the text view. I know that the content offset that I'm setting it to is correct.
After a lot of testing I finally realized that what was happening was that after the animation completed, the scroll view was automatically scrolling again.
This code does work:
UIView animateWithDuration:0.3
animations:^
{
self.textView.frame = frame;
}
completion:^(BOOL finished) {
[self.scrollView setContentOffset:CGPointMake(0, frame.origin.y) animated:YES];
}
];
but it looks strange because the scroll view scrolls to the wrong position, then to the right one.
Does anyone know how I can prevent the scroll view from changing it's content offset when the text view frame finishes its animation?
I am testing using iOS 5.0.
Here is a solution that I found that works. I'm still don't completely understand what's happening, possible it has something to do with the way my springs and struts are set. Basically I am shrinking the scroll view content size by the same amount that the text view shrinks.
- (void)keyboardDidShow:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// Get the height of the keyboard
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect fromView:nil];
CGSize kbSize = kbRect.size;
// Adjust the height of the text view to fit in the visible view
CGRect frame = self.textView.frame;
int visibleHeight = self.view.frame.size.height;
visibleHeight -= kbSize.height;
frame.size.height = visibleHeight;
// Get the new scroll view content size
CGSize contentSize = self.scrollView.contentSize;
contentSize.height = contentSize.height - self.textView.frame.size.height + frame.size.height;
[UIView animateWithDuration:0.1
animations:^
{
self.textView.frame = frame;
// Note that the scroll view content size needs to be reset, or the scroll view
// may automatically scroll to a new position after the animation is complete.
self.scrollView.contentSize = contentSize;
[self.scrollView setContentOffset:CGPointMake(0, frame.origin.y) animated:YES];
}
];
// Turn off scrolling in scroll view
self.scrollView.scrollEnabled = NO;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
// Update the view layout
[UIView animateWithDuration:0.3 animations:^
{
self.scrollView.contentOffset = CGPointZero;
[self updateViewLayout];
}
];
// Turn on scrolling in the scroll view
self.scrollView.scrollEnabled = YES;
}
([self updateViewLayout] is a method that returns the text view to the correct height, and resets the scroll view content size, as well as making sure all the other subviews are properly positioned).
Do not adjust the frame of the text view. All you need to do is scroll the scrollview. If you scroll the scrollview animated, then the textView will slide up animated. Just do the following:
[self.scrollView setContentOffset:CGPointMake(0, frame.origin.y) animated:YES];
If you need the scroll view to scroll at a specific rate then manually adjust the content offset in an animation block.(I haven't tested this, you may want to adjust the animation flag, or step through the content offset one pixel at a time if this doesn't work)
[UIView animateWithDuration:0.3f
animations:^{
[self.scrollView setContentOffset:CGPointMake(0, frame.origin.y) animated:YES];
} ];
I believe I also had something similar. I wanted my horizontal UIScrollView to go back to it's original offset (0,0), and every time I animated back to it, it would be just a couple pixels off, but as soon as someone touched the scrollview, it would continue and finish the animation (almost like lag in a video game).
Due to that, I tried putting it in GCD as the main queue and it worked perfectly fine after that. Here's the code:
- (void)setPageZero{
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0f
delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.scrollView setContentOffset:CGPointMake(0, self.scrollView.frame.origin.y) animated:YES];
}
completion:^(BOOL finished) {}];
});
}
What that basically does, is that it puts the animation into the main queue (kind of like a superhighway in the CPU/GPU) and it prioritizes it over other things!
Hope this helps future readers!
I'm having a similar problem. Basically I have a scroll view with three pages each containing a custom UIView class. The custom UIView class contains an image and a number of overlay views containing information about the image. The overlay views contain a number of UITextView fields.
I attach the custom views to the scroll view and then I load the image & overlays using block operations. I swipe to the right by deleting the first view, repositioning the next two views, and adding an new view to the right.
If I create the UITextField objects and disable scrolling as follows:
UITextView* view = [[UITextView alloc]
initWithFrame:CGRectMake(position.x,position.y,width,height)];
view.editable = NO;
view.scrollEnabled = NO;
Then the scroll view gets repositioned between pages when I return from the block code. Note that I don't reposition the scroll view and on exit from the block, the scroll view is in the correct position.
However, if I comment out the code to disable scrolling
UITextView* view = [[UITextView alloc]
initWithFrame:CGRectMake(position.x,position.y,width,height)];
view.editable = NO;
// view.scrollEnabled = NO;
this repositioning does not occur and the scrolling works as expected.
Complete hack: May improve the reset issue:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self setContentOffset:self.contentOffset animated:NO];
}

Resources