UIscrollView animation not smoothly - ios

Code:
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
int diff = end-start;
if (diff>0)
{
k = k + 95;
}
else {
k = k - 95;
}
[scrollView setContentOffset:CGPointMake(k, 0) animated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
start = scrollView.contentOffset.x;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
end = scrollView.contentOffset.x;
}
I have a scrollview with 3 labels. leftSide 90 pixels width, center 90 pixels width and rightside 90 pixels width.Each label has 5 pixel gap.when scrollview scroll left or right i align next or previous label center of the screen.Logic is working fine.but animtation is not smooth and also there is a pause after starting animation.How do i solve this problem?.How do i speed up animation?any help will be appreciated.

You can adjust the speed of scrolling by the following way, by adjusting duration value.
[UIView animateWithDuration:0.5 animations:^{
_scrolview.contentOffset = CGPointMake(k, 0);
}];

I think the better way would be to just use UIScrollView's
pagingEnabled = YES;
And you don't need the above delegate methods. Make sure you have set the contentSize.

Related

iOS UIView animation when table view scroll?

I want animation like home screen of this app.This screen has a view (with a label on it) on top and a table view under it. When i scroll the table the top view and title on it become small with animation and after a specific point it becomes like navigation bar with title in centre. And the same thing when i scroll down.
Here is what i tried so far
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
NSLog(#"y is::: %f", offset.y);
if (offset.y > 0 && offset.y < 16 && self.topView.frame.size.height > 64) {
[UIView animateWithDuration:0.8 animations:^{
CGRect rect = self.topView.frame;
rect.size.height = rect.size.height-offset.y;
self.topView.frame = rect;
}completion:^(BOOL finish){
}];
}
else if(offset.y <= 0 && offset.y > -16 && self.topView.frame.size.height < 80)
{
[UIView animateWithDuration:0.8 animations:^{
CGRect rect = self.topView.frame;
rect.size.height = 80;
self.topView.frame = rect;
}completion:^(BOOL finish){
}];
}
}
Note that my top view initial height is 80 and i want to make it 64 when scroll up and vice versa.
with this code the view height animated but it depends upon animation duration.So if i scroll fast then it is not working properly.The original app i mentioned have very smooth animation. How could i achieve that?
Screen shot of original App-
These screen shots showing three different scroll position, please note the "MySurgery" text on top.1- Larger text and right align.
2- small text and near centre position
3- smaller text and centralised(like navigation bar)
I think you dont need animation for this, you just need some calculations and setting frame of the top view. like,
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
if (offset.y>0 && offset.y<64) {
[topView layoutIfNeeded];
CGRect viewFrame = topView.frame;
viewFrame.size.height--;
topView.frame = viewFrame;
}
}
Note that layoutIfNeeded is required if your view is using autolayout

UIScrollView zoom is not working properly

Whenever I click on an image, I want the image to display as a larger image with the ability to zoom. So far when I click on the image, it displays as I want it to inside a scrollview. However, I have to get lucky to be able to zoom in properly. Most of the time when I attempt to zoom, the image just moves down and to the right and does not zoom at all. Here is my code:
-(void) pictureButtonAction
{
self.scrollImageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.scrollImageView.contentSize = self.fullImageView.image.size;
self.scrollImageView.delegate = self;
self.scrollImageView.clipsToBounds = YES;
self.scrollImageView.scrollEnabled = YES;
[self.scrollImageView setMaximumZoomScale:4.0f];
[self.scrollImageView setMinimumZoomScale:1.0f];
[self.view addSubview:self.scrollImageView];
[self.scrollImageView addSubview:fullImageView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.fullImageView;
}
ImageViews frame got changed while zooming , so that it moves down. So, You have to centralize the image view each time you zoom .
your scrollviews contentsize should be greater than or equal to your image size , if your fullImageView.image.size is less than your scrollviews bounds ,then set your scrollviews contentSize atleast double the scrollviews bounds .
call the below function in scrollViewDidZoom delegate method
-(void) centerScrollViewContents
{
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
Try this , hope it will help you ; happy coding ! :)
#Maria's answer works but when zooming you'll experience unwanted bounces, instead of using it under scrollViewDidZoom, use scrollViewDidEndZooming: delegate to prevent that..
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
[self centerScrollViewContents];
}
and with a little enhancement from her code:
[UIView animateWithDuration:0 animations:^{
self.previewImageView.frame = contentsFrame;
}];
a bit late for an answer but i hope this will be useful to some..

how to move one direction in UIScrollView when scrolling

I'm new in objective-c. I create UIScrollView object and add in my view with this code:
height = self.view.frame.size.height;
width = self.view.frame.size.width;
scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
scrollbar.delegate = self;
scrollbar.backgroundColor = [UIColor whiteColor];
scrollbar.maximumZoomScale = 1.0;
scrollbar.minimumZoomScale = 1.0;
scrollbar.clipsToBounds = YES;
scrollbar.showsHorizontalScrollIndicator = YES;
scrollbar.pagingEnabled = YES;
[scrollbar setContentSize:CGSizeMake(width*4, height*4)];
[self.view addSubview:scrollbar];
for (int i = 1; i <= 4; i++) {
first = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
first.view.frame = CGRectMake((i-1)*width, 0, width, height*4);
[scrollbar addSubview:first.view];
switch (i) {
ase 1:
first.view.backgroundColor = [UIColor blueColor];
break;
case 2:
first.view.backgroundColor = [UIColor redColor];
break;
case 3:
first.view.backgroundColor = [UIColor greenColor];
break;
case 4:
first.view.backgroundColor = [UIColor grayColor];
break;
default:
break;
}
}
in my code I add 4 view in my ScrollView with different color now I want when scrolling on my ScrollView detect dx & dy (dx: driving distance on Axis.x & dy:driving distance on Axis.y) and check these two variable and when :
Notic: I want when any one touch on ScrollView and moving touch on Axis (x or y) or touch on Both Axis (x and y) check this :
if (dx > dy) disable horizontal scroll and moving in vertical side!!!
else moving in horizontal side and disable vertical scroll!!!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect visibleRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.contentOffset.x + scrollView.bounds.size.width, scrollView.contentOffset.y + scrollView.bounds.size.height);
NSLog(#"%f,%f",visibleRect.origin.x,visibleRect.origin.y);
/*NSLog(#"x : %f",scrollView.contentOffset.x);
NSLog(#"y : %f",scrollView.contentOffset.y);*/
if (fabsf(scrollView.contentOffset.x) > fabsf(scrollView.contentOffset.y)) {
NSLog(#"Vertical Side");
}
else {
NSLog(#"Horizontal Side");
}
}
please guide me guys. I can't disable one side and move another side!!! thanks
If i understood you right, you want to disable scrolling in the direction that has been dragged less by the user. If so, UIScrollView already offers an option to do so:
scrollView.directionalLockEnabled = YES;
When this option is set to true UIScrollView will handle the correct direction lock by itself.
For more information look at the documentation: link
You can achieve the result you want adding some code to your delegate. This is my ViewController.m file. -viewDidLoad, #import statements and the other methods are omitted.
#interface ViewController () {
CGPoint initialOffset;
NSInteger direction; // 0 undefined, 1 horizontal, 2 vertical
}
#end
#implementation ViewController
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// retrieve current offset
CGPoint currentOffset = scrollView.contentOffset;
// do we know which is the predominant direction?
if (direction == 0) {
// no.
CGFloat dx = currentOffset.x - initialOffset.x;
CGFloat dy = currentOffset.y - initialOffset.y;
// we need to decide in which direction we are moving
if (fabs(dx) >= fabs(dy)) {
direction = 1; // horizontal
} else if (fabs(dy) > fabs(dx)) {
direction = 2;
}
}
// ok now we have the direction. update the offset if necessary
if (direction == 1 && currentOffset.y != initialOffset.y) {
// remove y offset
currentOffset.y = initialOffset.y;
// update
[scrollView setContentOffset:currentOffset];
} else if (direction == 2 && currentOffset.x != initialOffset.x) {
currentOffset.x = initialOffset.x;
[scrollView setContentOffset:currentOffset];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// store the current offset
initialOffset = scrollView.contentOffset;
// reset flag
direction = 0; // AKA undefined
}
#end
When you start dragging, the delegate will reset the flag direction to "unknown" state, and store the current content offset. After every dragging move, your -scrollViewDidScroll: will be called. There, you decide which is the predominant direction (if this hasn't been done yet) and correct the current scrolling offset accordingly, by removing the x (or y) offset.
I tested this with the same settings you provided, only I used a UIImageView inside UIScrollView and I set up everything via InterfaceBuilder, but it should work fine. Theoretically, with this method you could replace directionLock, but remember that -scrollViewDidScroll is called many times during an action, and every time it rewrites the content offset (if the scrolling is happening in both directions). So if you leave directionLock enabled, you save many of the calls to setContentOffset: that the delegate performs.

iOS UIScrollView pinch zoom adds white space

I'm implementing pinch to zoom of an image using UIImageView inside a UIScrollView. The scroll view doesn't take full screen size. I made the background of the scrollview green. I then pinch-zoomed in the image.
The problem is that when I scroll the zoomed image it's not centered correctly in the scroll view and has some space to the right and bottom. If I try to scroll to the top or left the scroll view doesn't allow to scroll to the end of the image by similar amount of space. The amount of space seems to be related on how I move my fingers when doing the pinch gesture.
Here is a screenshot:
Here is my code:
// On viewWillAppear:
- (void)loadMyImage
{
self.myImage.contentMode = UIViewContentModeScaleAspectFit;
self.myImage.image = self.originalImage; // an UIImage loaded from gallery
self.myImage.bounds = CGRectMake(0, 0, self.originalImage.size.width, self.originalImage.size.height);
self.scrollView.bounds = CGRectMake(0,0,320,200);
self.scrollView.frame = CGRectMake(0,0,320,200);
self.scrollView.minimumZoomScale=1.0f;
self.scrollView.maximumZoomScale=2.0f;
self.scrollView.delegate = self;
self.myImage.userInteractionEnabled = YES;
}
// Scroll view zooming events:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{return self.myImage;}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.myImage.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.myImage.center = CGPointMake(
contentsFrame.origin.x + contentsFrame.size.width / 2.0f,
contentsFrame.origin.y + contentsFrame.size.height / 2.0f);
CGPoint offset = CGPointMake(
self.myImage.center.x - self.scrollView.bounds.size.width / 2.0f,
self.myImage.center.y - self.scrollView.bounds.size.height / 2.0f);
[UIView animateWithDuration:.25 animations:^{
[self.scrollView setContentOffset:offset];
}];
NSLog(#"Final state of frame: %g x %g (%g , %g) and center (%g , %g)",
self.myImage.frame.size.width, self.myImage.frame.size.height,
self.myImage.frame.origin.x, self.myImage.frame.origin.y,
self.myImage.center.x, self.myImage.center.y);
NSLog(#"offset: (%g , %g)",
self.scrollView.contentOffset.x, self.scrollView.contentOffset.y);
NSLog(#"Inset left %g , right %g , top %g , bottom %g",
self.scrollView.contentInset.left,
self.scrollView.contentInset.right,
self.scrollView.contentInset.top,
self.scrollView.contentInset.bottom);
NSLog(#"==================================");
}
And here is my output:
Initial state of bounds: 320 x 200
of frame: 320 x 200 (0 , 0) and center (160 , 100)
offset: (0 , 0)
==================================
Final state of frame: 640 x 400 (0 , 0) and center (320 , 200)
offset: (160 , 100)
Inset left 0 , right 0 , top 0 , bottom 0
==================================
I tried setting automaticallyAdjustsScrollViewInsets to NO, but my view controller doesn't respond to the selector. And besides, the insets report 0-s.
Any solution or work-around would be great. Any idea or possible explanation for this behavior would also be appreciated.
I suggest you to use the following library in Github:
https://github.com/akhiljayaram/PJImageZoomView
Don't forget to add
self.automaticallyAdjustsScrollViewInsets = NO;
in your viewcontroller.
Hope this helps! :)

Adjust UICollectionView scrollOffset in intervals of the cell width

I have a collection view as such (scrolls sideways):
When the user scrolls, I want to adjust the table so it lines up perfectly around the static square.
I decided to do this using the scrollView when it ends dragging. However it is always just a little but off or at the very end completely off.
Where are my calculations going wrong?
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (![scrollView isKindOfClass:[IndexedCollectionView class]]) return;
IndexedCollectionView *collectionView = (IndexedCollectionView *)scrollView;
NSMutableArray *data = self.sectionsData[collectionView.index];
int cellsCount = data.count;
if (cellsCount==0) {
cellsCount++;
}
//Adjust cells so they line up correctly
CGPoint adjustedOffset = scrollView.contentOffset;
long interval = scrollView.contentSize.width/cellsCount;
long remainder = (long)adjustedOffset.x%interval;
long adjustment = interval-remainder;
adjustedOffset.x +=adjustment;
[UIView animateWithDuration:.3 animations:^{
[scrollView setContentOffset:adjustedOffset];
}];
NSInteger index = collectionView.index;
self.contentOffsetDictionary[[#(index) stringValue]] = #(adjustedOffset.x);
}

Resources