Niestandardowe stronicowanie UIScrollView z scrollViewWillEndDragging
Próbuję użyć nowego scrollViewWillEndDragging: withVelocity: targetContentOffset: uiscrollview delegate call w iOS 5, ale nie mogę go właściwie odpowiedzieć na mnie. Zmieniam wartość targetContentOffset - > X, ale nigdy nie zostanie użyta. Wiem, że kod jest uruchamiany, ponieważ uderzy w punkty przerwania w tej funkcji. Próbowałem nawet ustawić wartość offsetu na ciężko zakodowany numer, więc wiem, gdzie to się skończy, ale nigdy działa.
Czy ktoś był w stanie użyć tego poprawnie i sprawić, że zadziała? Czy jest jakieś inne wezwanie delegata, które musi zostać wdrożone, aby to zadziałało?
Oto Mój kod na wypadek, gdyby ktoś zobaczył z nim coś nie tak:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
// goodOffsetX returns the contentOffset i want the scrollView to stop at
CGFloat goodOffsetX = [self _horizontalContentOffsetForTargetHorizontalContentOffset:(*targetContentOffset).x velocity:velocity.x];
NSLog( @" " );
NSLog( @"scrollViewWillEndDragging" );
NSLog( @" velocity: %f", velocity.x );
NSLog( @" currentX: %f", scrollView.contentOffset.x );
NSLog( @" uikit targetX: %f", (*targetContentOffset).x );
NSLog( @" pagedX: %f", goodOffsetX );
targetContentOffset->x = goodOffsetX;
}
5 answers
Możesz zaimplementować niestandardowe stronicowanie za pomocą tego kodu:
- (float) pageWidth {
return ((UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout).itemSize.width +
((UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout).minimumInteritemSpacing;
}
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
CGFloat pageWidth = self.collectionView.frame.size.width + 10 /* Optional Photo app like gap between images. Or use [self pageWidth] in case if you want the next page be also visible */;
_currentPage = floor((self.collectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@"Dragging - You are now on page %i", _currentPage);
}
- (void) scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {
CGFloat pageWidth = self.collectionView.frame.size.width + 10; // [self pageWidth]
int newPage = _currentPage;
if (velocity.x == 0) { // slow dragging not lifting finger
newPage = floor((targetContentOffset->x - pageWidth / 2) / pageWidth) + 1;
}
else {
newPage = velocity.x > 0 ? _currentPage + 1 : _currentPage - 1;
if (newPage < 0)
newPage = 0;
if (newPage > self.collectionView.contentSize.width / pageWidth)
newPage = ceil(self.collectionView.contentSize.width / pageWidth) - 1.0;
}
NSLog(@"Dragging - You will be on %i page (from page %i)", newPage, _currentPage);
*targetContentOffset = CGPointMake(newPage * pageWidth, targetContentOffset->y);
}
Oczywiście musisz ustawić pagingEnabled = NO. _currentPage jest klasą iVar. Dzięki http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html za wskazywanie właściwej drogi.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-07 22:39:06
SWIFT 3
Z demo tutaj https://github.com/damienromito/CollectionViewCustom
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageWidth = Float(itemWidth + itemSpacing)
let targetXContentOffset = Float(targetContentOffset.pointee.x)
let contentWidth = Float(collectionView!.contentSize.width )
var newPage = Float(self.pageControl.currentPage)
if velocity.x == 0 {
newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) + 1.0
} else {
newPage = Float(velocity.x > 0 ? self.pageControl.currentPage + 1 : self.pageControl.currentPage - 1)
if newPage < 0 {
newPage = 0
}
if (newPage > contentWidth / pageWidth) {
newPage = ceil(contentWidth / pageWidth) - 1.0
}
}
let point = CGPoint (x: CGFloat(newPage * pageWidth), y: targetContentOffset.pointee.y)
targetContentOffset.pointee = point
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-08 15:25:50
Udało mi się przeprowadzić szybki test i dostałem to, aby poprawnie wystrzelić i zatrzymać mój obiekt zgodnie z życzeniem. Zrobiłem to za pomocą następującego prostego testu:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
targetContentOffset->x = scrollView.contentOffset.x - 10;
}
Wydaje się, że ta metoda prawdopodobnie nie jest problemem w Twoim kodzie, ale bardziej prawdopodobne jest, że twój 'goodOffsetX' nie oblicza poprawnie prawidłowej wartości, na której należy się zatrzymać.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-13 03:35:24
Swift 2.2:
extension SomeCollectionViewController {
override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
let pageWidth = Float(collectionView!.frame.size.width)
let xCurrentOffset = Float(collectionView!.contentOffset.x)
currentPage = floor((xCurrentOffset - pageWidth / 2) / pageWidth) + 1
}
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageWidth = Float(collectionView!.frame.size.width)
let targetXContentOffset = Float(targetContentOffset.memory.x)
let contentWidth = Float(collectionView!.contentSize.width)
var newPage = currentPage
if velocity.x == 0 {
newPage = floor((targetXContentOffset - pageWidth / 2) / pageWidth) + 1
} else {
newPage = velocity.x > 0 ? currentPage + 1 : currentPage - 1
if newPage < 0 {
newPage = 0
}
if newPage > contentWidth / pageWidth {
newPage = ceil(contentWidth / pageWidth) - 1.0
}
}
targetContentOffset.memory.x = CGFloat(newPage * pageWidth)
}
}
Użyłem również collectionView?.decelerationRate = UIScrollViewDecelerationRateFast
zgodnie z sugestią @skagedal, aby poprawić szybkość strony.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-05 21:40:41
public func scrollViewWillEndDragging(_ scrollView: UIScrollView ,withVelocity velocity: CGPoint, targetContentOffset:
UnsafeMutablePointer<CGPoint>){
let pageWidth = Float(appWidth + itemSpacing)
let targetXContentOffset = Float(targetContentOffset.pointee.x)
var newPage = Float(currentPageIndex)
// I use this way calculate newPage:
newPage = roundf(targetXContentOffset / pageWidth);
//if velocity.x == 0 {
// newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) + 1.0
//} else {
// newPage = Float(velocity.x > 0 ? newPage + 1 : newPage - 1)
// if newPage < 0 {
// newPage = 0
// }
// if (newPage > contentWidth / pageWidth) {
// newPage = ceil(contentWidth / pageWidth) - 1.0
// }
//}
let targetOffsetX = CGFloat(newPage * pageWidth)
let point = CGPoint (x: targetOffsetX, y: targetContentOffset.pointee.y)
targetContentOffset.pointee = point
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-14 12:02:43