When UITextView is scrolling ...? - ios

I have a textView which is not editable so my textView is only readable. And there is a ImageView on the top of textView. I want that, when the textView is scrolling, ImageView (Y position) disappear slowly depends on How many the textView scrolled.. If I should give an example, it looks like Twitter "Me" page but non blurred. I hope I was clear.

First of call UIScrollViewDelegate, make your viewcontroller delegate to self.Because it's subclass of UIScrollview.
and implement scrollView function according to your need.Suppose you want to disappear image after scrolling finished. then implement this method.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"Finished scrolling");
}

Related

Easy zooming UIScrollView

I have an UIScrollView with many buttons and some labels. My question is: How can I easily zoom UIScrollView content (with two fingers)?
Thank you very much.
UIScrollViews already support zoom using pinch gesture, you just need to implement the delegate method
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the view that you want to zoom
return self.viewZoom;
}
Returning the view you want to zoom.
As Andrea pointed out the UIScrollView already supports zoom feature.
Follow these steps:
Add a UIView (say viewZoom) to the scrollView by giving same width and height as that of scroll view.
Add all UI components (Buttons etc) to viewZoom.
Set maximumZoomScale property of scroll view to '2' (or any other value as per your requirement).
Set your view controller as delegate to ScrollView(scrollView.delegate = self).
Implement - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollViewin view controller and return the "viewZoom" you added to the scroll view from this method.

ScrollviewDidScroll not called at each point

There is a button at the bottom of my view controller. When the user scrolls down the button has to be attached to the scrollview at certain height.
I need to attach a button to the scrollview, immediately when the contentOffset.y reaches a particular value. -(void) scrollviewDidScroll doesn't help me as there might be a jump in contentOffset when the user is scrolling fast. Any leads on this are helpful.
Also, whenever I add a subview to the scrollview, -(void) viewDidLayoutSubviews is called. Which in turn sets the contentOffset to {0,0}. How can I achieve the functionality I need?
I needed to do the same thing with a UITableView and for me using scrollViewDidScroll worked.
I created a view called staticBar and added it as a subview of the tableView, but I had to rearrange the tableview subviews for it to appear in the right place. I don't have my code in front of me, but in -scrollViewDidScroll: it looked something like this:
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
CGFloat staticBarAdjustedY = _staticBarY - scrollView.contentOffset.y;
CGFloat scrollViewYFloor = scrollView.frame.size.height - _staticBar.frame.size.height;
// This way maximum Y the view can have is at the base of the scrollView
CGFloat newY = MIN( staticBarAdjustedY, scrollViewYFloor);
_staticBar.frame = (CGRect){ { _staticBar.frame.origin.x, newY}, _staticBar.frame.size}
}
I will check my code later today and add more details here.
Also, you said the scrollviewDidScroll has jumps in contentOffset, but it's worth mentioning that these jumps are the same that the scrollView uses to scroll its own view. So it's not like you are "losing" frames on this delegate method.
Hope it helps.
PS: So, here is the rest of my code.
//I place my custom view as a subview of the tableView below it's last subview
//The last subview is for scroll indicators.
WTButtonsBar *buttonBar = [[WTButtonsBar alloc] init];
[self.tableView insertSubview:buttonBar belowSubview:self.tableView.subviews.lastObject];
In scrollViewDidScroll:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//In my app I needed my view to stick to the top of the screen
//thats why I use MAX here
//self.buttonsBarOriginalY is the view's position in the scrollView when it isn't attached to the top.
CGFloat newY = MAX(scrollView.contentOffset.y, self.buttonsBarOriginalY)
[_buttonsBar setFrame:(CGRect){{0, newY}, _buttonsBar.frame.size}];
}

Make UIView under a UITableView visible when you scroll

I have a UITableView that's placed on top of a UIView.
So basically, it looks like this:
I have a tableviewHolder which has two subviews
1.) UIView with other subviews
2.) UITableView
Whenever the user scrolls the UITableView, i want the UIView beneath it to be visible.
I tried setting the UITableView's background to clear and it works. However, if the tableview's background is clear, then other portion of the UIView beneath it would be visible when the rows of the cell are not enough to cover the whole table.
How can I change the background color of the table view and still make the UIView beneath it visible whenever the user scrolls?
Use
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
(ref https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intfm/UIScrollViewDelegate/scrollViewWillBeginDragging: )
This method will be called when the UITableView begins scrolling. So, in this method, set the UIView to visible
view.setAlpha=1.0;
and in
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
set the uIView to invisible again by using
view.setAlpha=0.0;
Am asking you to use UIScrollViewDelegate methods in a UITableViewDelegate class. This will work because UITableViewDelegate inherits from UIScrollViewDelegate. So all methods in UIScrollViewDelegate will also work for UITableViewDelegate.

UIScrollView scrolled after setContentSize

So I want to create some detail content in UIView(320,470) that taller than Viewport (320, 367).
I create it separated in IB like (see pic.). Everything looks OK until I setContentSize to make the UIScrollView scrollable..
This is my code placed in ViewDidLoad
CGRect frame = self.uiContent.frame;
[self.uiScrollView addSubview:self.uiContent];
[self.uiScrollView setContentSize:frame.size];
The content is scrolled with animation to middle after setContentSize is called.. How to prevent that auto-scroll?
I found the culprit.. It was UITextView. Sorry if I don't mention I use UITextView for multiline label under address label.
Quoting "Taketo Sano" on other question : https://stackoverflow.com/a/5673026/453407
I've investigated how the auto-scroll is done by tracking the
call-trace, and found that an internal [UIFieldEditor
scrollSelectionToVisible] is called when a letter is typed into the
UITextField. This method seems to act on the UIScrollView of the
nearest ancestor of the UITextField.
UIScrollView is auto scrolled to UITextView when UITextView text is changed. So I found the solution by subclassing UIScrollview and override
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
and return nothing to disable the auto scroll... If you plan to use it in future, just use a bool variable to enable / disable it by using
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
if (!self.disableAutoScroll) {
[super scrollRectToVisible:rect animated:animated];
}
}
so you can disable the autoscroll before you change the UITextView by code and enable it after.

UIScrollView or UIImageView?

I need to place a photo in the screen and it should be able to be scrolled or zoomed,
I know UIScrollView does these, so I alloced one, but how about my photo?
I guess I should not just set the photo as the backgroundColor of my UIScrollView, and I haven't found any property of UIScrollView, in the document, to hold an image, such as "imageView", "contentView", or something like that.
So, what should I do to make a picture in my screen and make it able to respond to finger touches?
Thanks a lot!
Add the UIImageView to the UIScrollView and set the min/max scale value to it.
Then implement the following UIScrollViewDelegate function.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return yourImageView;
}
If you want to have multiple items scaled, add all the view into a UIView, and put that UIView inside the UIScrollView. Of course you have to change the return view to that UIView in the above function.
Just add your UIImageView as a subview of the UIScrollView. Also, set the scroll view's contentSize property to be equal to the size of the image view's frame.
THe scrollView is the first step.
Then you should add a UIImageView and load an image inside.
You need also to set the max e min zoomScale properties of the scrollview
And you must implement the scrollview delegate method – viewForZoomingInScrollView:

Resources