Here are the relevant methods from a UICollectionViewLayout subclass. You can
just subclass the standard flow layout and add this or you can make a more
custom layout if desired.
For context, this collectionView has a vertical strip of digits (multiple
strips make an odometer… like the pickerView but the strips cause each other to
increment when rolling past certain points i.e. ‘0’’9’ becomes ‘1’’0’). The
code shown here causes it to stop so that a digit is always centered after the
user scrolls it.
The -centeredRowForOffset: method (my helper function) returns which row it
should snap to based on the given scroll offset. The targetContentOffset…
methods tell the collectionView to actually snap to the offset for that row
when animating and scrolling (respective to the order shown below). They are
given a proposed offset to stop at, and you return the adjusted offset that you
would like.
Hope it helps!
func centeredRowForOffset(offset:CGFloat)->Int {
return Int(floor((offset / digitHeight) + 0.5))
}
override func
targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint) ->
CGPoint {
let row = centeredRowForOffset(proposedContentOffset.y)
let target = CGFloat(row) * digitHeight
return CGPoint(x: proposedContentOffset.x, y: target)
}
override func
targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint,
withScrollingVelocity velocity: CGPoint) -> CGPoint {
let row = centeredRowForOffset(proposedContentOffset.y)
let target = CGFloat(row) * digitHeight
return CGPoint(x: proposedContentOffset.x, y: target)
}
Thanks,
Jon
> On Dec 2, 2015, at 6:16 PM, Eric Dolecki <[email protected]> wrote:
>
> Do you have any sample code for such a thing?
>
> Sent from Outlook <http://aka.ms/Ox5hz3>
>
>
>
> On Wed, Dec 2, 2015 at 6:15 PM -0800, "Jon Hull" <[email protected]
> <mailto:[email protected]>> wrote:
>
> Hi Eric,
>
> When creating similar controls in the past (I am actually making an odometer
> style view right now), I have found that collection views work really well.
> There is a delegate call (something like proposedOffset:forTargetOffset:)
> which gives you the sticky behavior you desire...
>
> Thanks,
> Jon
>
> Sent from my iPhone
>
> > On Dec 2, 2015, at 4:22 PM, Eric Dolecki wrote:
> >
> > I need to create a time picker control but don't have much vertical room.
> > So buttons above and below to affect hours, min, am/pm are out. I was
> > thinking swipes up and down. Best to use 3 UIScrollviews? Inertia is there.
> > Only thing is how to get the numbers to "stick" in selection position while
> > still allowing for smooth scrolling with quick flicking. Technique for that?
> > Is this a good solution? Anything I might consider?
> > _______________________________________________
> >
> > Cocoa-dev mailing list ([email protected])
> >
> > Please do not post admin requests or moderator comments to the list.
> > Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> >
> > Help/Unsubscribe/Update your Subscription:
> > https://lists.apple.com/mailman/options/cocoa-dev/jhull%40gbis.com
> >
> > This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]