Is there a way to make it so the user can only scroll up in a table view? Right now I only see an option to scroll both up and down.
What does you mean under "scroll only up"?
If it's mean that content start from bottom and user can only scroll up, you can try this:
1) reverse array of objects that you use for feeding tableView
if shouldReverse {
var reversed = myObjects.reverse()
myObjects.removeAll(keepCapacity: false)
myObjects.splice(reversed, atIndex: 0)
}
2) implement scrollViewDidScroll: method from UIScrollViewDelegate protocol, and restrict content offset by it's y position
func scrollViewDidScroll(scrollView: UIScrollView) {
if shouldReverse && scrollView.contentOffset.y < 0 {
scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0)
}
}
Related
I am attempting to scale dot of the current page to be larger than the dots which are not 'selected'.
I am using the scrollview delegate to ascertain which page is current.
At the moment there is no change to the size of the dot.
How would I go about achieving this?
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var i = 0
if(scrollView.contentOffset.x < scrollView.frame.width){
pageControl.currentPage = 0
pageControl.subviews.forEach {
if(i == 0){
print("Edit")
$0.transform.scaledBy(x: 5, y: 5)
}
pageControl.layoutIfNeeded()
i += 1
}
}else{
pageControl.currentPage = 1
}
}
UIPageControl is not that customizable. Use a library like this one
TAPageControl
Is works the same way as UIPageControl and you can easily customize current and non current page dots by setting the properties dotImage and currentDotImage.
I have an UIScrollView with some labels inside. I can move the scroll view to the other 'page' with an button. But the offset isn't right when I push it too fast.
My code to move the scrollview to the next page:
#IBAction func moveToRight(_ sender: Any) {
let size = Int(scrView.contentOffset.x) + Int(scrView.frame.size.width);
scrView.setContentOffset(CGPoint(x: size, y: 0), animated: true)
}
The offset isn't right when I push it too fast. It looks like that the current animation stops and is going to perform the next one from it's current (unfinished) position.
Does anybody has an solution for this?
I don't have time to test but I think you are on the right track about the main reason of the issue. Since animated is set to true, my guess is contentOffset.x has not yet been set to its ultimate value until animation is finished.
Why don't you change your logic a bit and create a property which remembers the current page at where you last scrolled:
var currentPage: Int = 0
Then every time you moved right, increment the current page number if possible:
#IBAction func moveToRight(_ sender: Any) {
let maxX = scrollView.contentSize.x - scrollView.frame.width
let newX = CGFloat(currentPage + 1) * scrollView.frame.width
if newX <= maxX {
currentPage = currentPage + 1
scrollView.setContentOffset(CGPoint(x: newX, y: 0), animated: true)
}
}
I have a view hierarchy similar to the one in the image below (blue is the visible part of the scene):
So I have a UIScrollView with a lot of elements, out of which I am only showing the two button since they are relevant to the question. The first button is visible when the app is run, whereas the other one is positioned outside of the initially visible area. The first button is also the preferredFocusedView.
Now I am changing focus between the two buttons using a UIFocusGuide, and this works (checked it in didUpdateFocusInContext:). However, my scroll view does not scroll down when Button2 gets focused.
The scroll view is pinned to superview and I give it an appropriate content size in viewDidLoad of my view controller.
Any ideas how to get the scroll view to scroll?
Take a look at UIScrollView.panGestureRecognizer.allowedTouchTypes. It is an array of NSNumber with values based on UITouchType or UITouch.TouchType (depending on language version). By default allowedTouchTypes contains 2 values - direct and stylus. It means that your UIScrollView instance will not response to signals from remote control. Add the following line to fix it:
Swift 4
self.scrollView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouchType.indirect.rawValue)]
Swift 4.2 & 5
self.scrollView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]
Also, don't forget to set a correct contentSize for UIScrollView:
self.scrollView.contentSize = CGSize(width: 1920.0, height: 2000.0)
Finally I solved this by setting scrollView.contentSize to the appropriate size in viewDidLoad.
You need to add pan gesture recognizer. I learned from here: http://www.theappguruz.com/blog/gesture-recognizer-using-swift. I added more code to make it not scrolling strangely, e.g. in horizontal direction.
var currentY : CGFloat = 0 //this saves current Y position
func initializeGestureRecognizer()
{
//For PanGesture Recoginzation
let panGesture: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("recognizePanGesture:"))
self.scrollView.addGestureRecognizer(panGesture)
}
func recognizePanGesture(sender: UIPanGestureRecognizer)
{
let translate = sender.translationInView(self.view)
var newY = sender.view!.center.y + translate.y
if(newY >= self.view.frame.height - 20) {
newY = sender.view!.center.y //make it not scrolling downwards at the very beginning
}
else if( newY <= 0){
newY = currentY //make it scrolling not too much upwards
}
sender.view!.center = CGPoint(x:sender.view!.center.x,
y:newY)
currentY = newY
sender.setTranslation(CGPointZero, inView: self.view)
}
I want to implement "cards" in my app like Safari tabs or App Store search.
I will show user one card in a center of screen and part of previous and next cards at left and right sides. (See App Store search or Safari tabs for example)
I decided to use UICollectionView, and I need to change page size (didn't find how) or implement own layout subclass (don't know how)?
Any help, please?
Below is the simplest way that I've found to get this effect. It involves your collection view and an extra secret scroll view.
Set up your collection views
Set up your collection view and all its data source methods.
Frame the collection view; it should span the full width that you want to be visible.
Set the collection view's contentInset:
_collectionView.contentInset = UIEdgeInsetsMake(0, (self.view.frame.size.width-pageSize)/2, 0, (self.view.frame.size.width-pageSize)/2);
This helps offset the cells properly.
Set up your secret scrollview
Create a scrollview, place it wherever you like. You can set it to hidden if you like.
Set the size of the scrollview's bounds to the desired size of your page.
Set yourself as the delegate of the scrollview.
Set its contentSize to the expected content size of your collection view.
Move your gesture recognizer
Add the secret scrollview's gesture recognizer to the collection view, and disable the collection view's gesture recognizer:
[_collectionView addGestureRecognizer:_secretScrollView.panGestureRecognizer];
_collectionView.panGestureRecognizer.enabled = NO;
Delegate
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.x = contentOffset.x - _collectionView.contentInset.left;
_collectionView.contentOffset = contentOffset;
}
As the scrollview moves, get its offset and set it to the offset of the collection view.
I blogged about this here, so check this link for updates: http://khanlou.com/2013/04/paging-a-overflowing-collection-view/
You can subclass UICollectionViewFlowLayout and override like so:
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)contentOffset
withScrollingVelocity:(CGPoint)velocity
{
NSArray* layoutAttributesArray =
[self layoutAttributesForElementsInRect:self.collectionView.bounds];
if(layoutAttributesArray.count == 0)
return contentOffset;
UICollectionViewLayoutAttributes* candidate =
layoutAttributesArray.firstObject;
for (UICollectionViewLayoutAttributes* layoutAttributes in layoutAttributesArray)
{
if (layoutAttributes.representedElementCategory != UICollectionElementCategoryCell)
continue;
if((velocity.x > 0.0 && layoutAttributes.center.x > candidate.center.x) ||
(velocity.x <= 0.0 && layoutAttributes.center.x < candidate.center.x))
candidate = layoutAttributes;
}
return CGPointMake(candidate.center.x - self.collectionView.bounds.size.width * 0.5f, contentOffset.y);
}
This will get the next or previous cell depending on the velocity... it will not snap on the current cell however.
#Mike M's answer in Swift…
class CenteringFlowLayout: UICollectionViewFlowLayout {
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView,
let layoutAttributesArray = layoutAttributesForElementsInRect(collectionView.bounds),
var candidate = layoutAttributesArray.first else { return proposedContentOffset }
layoutAttributesArray.filter({$0.representedElementCategory == .Cell }).forEach { layoutAttributes in
if (velocity.x > 0 && layoutAttributes.center.x > candidate.center.x) ||
(velocity.x <= 0 && layoutAttributes.center.x < candidate.center.x) {
candidate = layoutAttributes
}
}
return CGPoint(x: candidate.center.x - collectionView.bounds.width / 2, y: proposedContentOffset.y)
}
}
A little edit on Soroush answer, which did the trick for me.
The only edit I made instead of disabling the gesture:
[_collectionView addGestureRecognizer:_secretScrollView.panGestureRecognizer];
_collectionView.panGestureRecognizer.enabled = NO;
I disabled scrolling on the collectionview:
_collectionView.scrollEnabled = NO;
As disabling the gesture disabled the secret scrollview gesture as well.
I'll add another solution. The snapping in place is not perfect (not as good as when paging enabled is set, but works well enough).
I have tried implementing Soroush's solution, but it doesn't work for me.
Because the UICollectionView is a subclass of UIScrollView it will respond to an important UIScrollViewDelegate method which is:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
The targetContentOffset (an inout pointer) lets you redefine the stopping point for a collection view (the x in this case if you swipe horizontally).
A quick note about a couple of the variables found below:
self.cellWidth – this is your collection view cell's width (you can even hardcode it there if you want)
self.minimumLineSpacing – this is the minimum line spacing you set between the cells
self.scrollingObjects is the array of objects contained in the collection view (I need this mostly for the count, to know when to stop scrolling)
So, the idea is to implement this method in the collection view's delegate, like so:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
if (self.currentIndex == 0 && velocity.x < 0) {
// we have reached the first user and we're trying to go back
return;
}
if (self.currentIndex == (self.scrollingObjects.count - 1) && velocity.x > 0) {
// we have reached the last user and we're trying to go forward
return;
}
if (velocity.x < 0) {
// swiping from left to right (going left; going back)
self.currentIndex--;
} else {
// swiping from right to left (going right; going forward)
self.currentIndex++;
}
float xPositionToStop = 0;
if (self.currentIndex == 0) {
// first row
xPositionToStop = 0;
} else {
// one of the next ones
xPositionToStop = self.currentIndex * self.cellWidth + (self.currentIndex + 1) * self.minimumLineSpacing - ((scrollView.bounds.size.width - 2*self.minimumLineSpacing - self.cellWidth)/2);
}
targetContentOffset->x = xPositionToStop;
NSLog(#"Point of stopping: %#", NSStringFromCGPoint(*targetContentOffset));
}
Looking forward to any feedback you may have which makes the snapping in place better. I'll also keep on looking for a better solution...
The previous is quite complicated, UICollectionView is a subclass of UIScrollView, so just do this:
[self.collectionView setPagingEnabled:YES];
You are all set to go.
See this detailed tutorial.
I did google enough, & I did check posts like these ( Finding the direction of scrolling in a UIScrollView? ) in stackoverflow before posting this. I have a dynamic number of photos in an iPhone App, that am displaying through UIScrollView. At any point in time, I have only 3 photos being displayed in the scroll-view. When I have, say 4 photos, in total:
1st photo : displayed at offset 0.0
2nd photo : displayed at offset 320.0
3rd photo : displayed at offset 640.0
Now, when the user scrolls to the 4th photo, the scroll-view resets to 0.0 offset. If the user tries to scroll 'beyond' the 4th photo, scrolling should stop in the right-direction only (so that user doesn't scroll 'beyond'). But currently, the user 'is able' to scroll beyond the last photo ; however, I detect this programmatically & reset the offset. But it doesn't look neat, as the user sees the black background momentarily. I want to detect that the user has started scrolling 'right' (remember, scrolling 'left' i.e. to the 'previous' photo is okay) in scrollViewWillBeginDragging, so that I can stop any further scrolling to the right.
What I tried:
Trying using self.scrollView.panGestureRecognizer's
translationInView isn't working, because there is no
panGestureRecognizer instance returned in the first place (!),
though the UIScrollView API claims so.
Detecting this in scrollViewDidEndDecelerating is possible, though
it'll not serve my purpose.
I had no issues determining direction in scrollViewWillBeginDragging when checking the scroll view's panGestureRecognizer:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
if(translation.y > 0)
{
// react to dragging down
} else
{
// react to dragging up
}
}
I found it very useful in canceling out of a scroll at the very first drag move when the user is dragging in a forbidden direction.
Swift 3 Solution
1- Add UIScrollViewDelegate
2- Add this code
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
let actualPosition = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
if (actualPosition.y > 0){
// Dragging down
}else{
// Dragging up
}
}
For swift 2.0+ & ios 8.0+
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
let actualPosition = scrollView.panGestureRecognizer.translationInView(scrollView.superview)
if (actualPosition.y > 0){
// Dragging down
}else{
// Dragging up
}
}
Thank you, Kris. This is what worked for me, finally:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Detect the scroll direction
if (lastContentOffset < (int)scrollView.contentOffset.x) {
...
}
}
This is what I used and it works nicely at least on iOS 6.0:
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView];
// Detect direction by accessing x or y of translation
}
Saves you the instance variable for lastContentOffset ...
Take a look at scrollViewWillEndDragging:withVelocity:targetContentOffset:.
You can use that method do do any checking and see if it is going where it should not and then in the method you can set a new targetContentOffset.
Per the documentation:
This method is not called when the value of the scroll view’s pagingEnabled property is YES. Your application can change the value of the targetContentOffset parameter to adjust where the scrollview finishes its scrolling animation.
There seem to be issues with detecting scroll direction based on the translation of the scrollView's pan recognizer in iOS 7+. This seems to be working pretty seamlessly for my purposes
func scrollViewDidScroll(scrollView: UIScrollView) {
if !scrollDirectionDetermined {
let translation = scrollView.panGestureRecognizer.translationInView(self.view)
if translation.y > 0 {
println("UP")
scrollDirectionDetermined = true
}
else if translation.y < 0 {
println("DOWN")
scrollDirectionDetermined = true
}
}
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
scrollDirectionDetermined = false
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
scrollDirectionDetermined = false
}
scrollView.panGestureRecognizer translationInView:scrollView doesn't report anything useful in scrollViewWillBeginDragging in iOS 7.
This does:
In the #interface
BOOL scrollDirectionDetermined;
and:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (!scrollDirectionDetermined) {
if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x > 0) {
//scrolling rightwards
} else {
//scrolling leftwards
}
scrollDirectionDetermined = YES;
}
}
and:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
scrollDirectionDetermined = NO;
}
Building off of #Oscar's answer, you can do things like
scrollView.bounces = actualPosition.y < 0 if you want the scrollView to bounce when you scroll to the bottom but not when you scroll to the top