UIScrollview,垂直分页工作,但用户未登陆到下一页

UIScrollview with paging vertically working but user not landing to the next page

提问人:stefanosn 提问时间:6/13/2022 更新时间:6/14/2022 访问量:45

问:

我正在使用以下代码滚动到具有 scrollview 的 UITableView 的下一页。滚动有效,但用户登陆的页面取决于他用手指滚动的力度。无论滚动的力量如何,它都需要降落到下一页。我怎样才能做到这一点?任何帮助都值得赞赏...

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint
*)targetContentOffset {
    self.tableView.rowHeight = [UIScreen mainScreen].bounds.size.height-self.navigationController.navigationBar.frame.size.height-(self.appDelegate.heightOfEightFeedWalletTopConstraint*2)+12;

    int tomove = ((int)targetContentOffset->y % (int)self.tableView.rowHeight);
    if(tomove < self.tableView.rowHeight/6 || velocity.y < 0){
            targetContentOffset->y -= tomove;
       }
        else{
            targetContentOffset->y += (self.tableView.rowHeight-tomove);
        }
}
Objective-C UITableView UIscrollView

评论


答:

1赞 DonMag 6/14/2022 #1

你不需要任何代码......

您需要做的就是在滚动视图上启用分页。

这将

  • 创建一个在所有 4 个边上都有 20 个点的滚动视图
  • 添加垂直堆栈视图
  • 添加 10 个标签作为排列的子视图(每个标签将是滚动视图框架的高度)

滚动视图已启用分页:

#import <UIKit/UIKit.h>

@interface PageScrollViewController : UIViewController
{
    UIScrollView *scrollView;
}
@end

@implementation PageScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    scrollView = [UIScrollView new];
    [scrollView setPagingEnabled:YES];
    
    UIStackView *stack = [UIStackView new];
    stack.axis = UILayoutConstraintAxisVertical;
    stack.distribution = UIStackViewDistributionFillEqually;

    stack.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;

    [scrollView addSubview:stack];
    [self.view addSubview:scrollView];
    
    // respect safeArea
    UILayoutGuide *g = self.view.safeAreaLayoutGuide;
    UILayoutGuide *cg = scrollView.contentLayoutGuide;
    
    [NSLayoutConstraint activateConstraints:@[
        
        [scrollView.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
        [scrollView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:20.0],
        [scrollView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-20.0],
        [scrollView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:-20.0],
        
        [stack.topAnchor constraintEqualToAnchor:cg.topAnchor constant:0.0],
        [stack.leadingAnchor constraintEqualToAnchor:cg.leadingAnchor constant:0.0],
        [stack.trailingAnchor constraintEqualToAnchor:cg.trailingAnchor constant:0.0],
        [stack.bottomAnchor constraintEqualToAnchor:cg.bottomAnchor constant:0.0],
        
        [stack.widthAnchor constraintEqualToAnchor:scrollView.frameLayoutGuide.widthAnchor],
        
    ]];
    
    // now let's add some views to the stack view
    //  making each one the same height as the scroll view frame
    NSArray <UIColor *> *colors = @[
        UIColor.yellowColor,
        UIColor.greenColor,
        UIColor.cyanColor,
        UIColor.orangeColor,
    ];
    
    for (int i = 0; i < 10; i++) {
        UILabel *v = [UILabel new];
        v.textAlignment = NSTextAlignmentCenter;
        v.text = [NSString stringWithFormat:@"Label %d", i];
        v.backgroundColor = colors[i % colors.count];
        [stack addArrangedSubview:v];
        [v.heightAnchor constraintEqualToAnchor:scrollView.frameLayoutGuide.heightAnchor].active = YES;
    }

}

@end

评论

0赞 stefanosn 6/14/2022
非常感谢先生的回复。我很感激。我会尝试你的代码并让你知道。