跟随 UICollectionView 指示器的 UIView [英] UIView that follows UICollectionView indicator

查看:18
本文介绍了跟随 UICollectionView 指示器的 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含 UIImageView 和 UILabel 的自定义 UIView,指向 UICollectionView 滚动指示器,并随其滚动.

I want to create a custom UIView that contains a UIImageView and a UILabel, that points to the UICollectionView scroll indicator, and scrolls with it.

我正在使用此代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //get refrence of vertical indicator
    UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
    // get the relative position of the indicator in the main window
    CGPoint p = [verticalIndicator convertPoint:verticalIndicator.bounds.origin toView:[[UIApplication sharedApplication] keyWindow]];
    // set the custom view new position
    CGRect indicatorFrame = self.indicatorView.frame;
    indicatorFrame.origin.y = p.y;
    self.indicatorView.frame = indicatorFrame;
}

但是indicatorView 并没有完全遵循指标!!

But the indicatorView does not follow the indicator exactly !!

推荐答案

这对我有用:请查看随附的 gif.我添加了与滚动指示器平行的蓝色自定义 uiview.

This worked for me: Please look at the attached gif. I have added the custom uiview with blue color parallel to the scrool indicator.

我尝试了表格视图,但这也适用于集合视图.

I tried for the table view, but this also works for the collection view too.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    dispatch_async(dispatch_get_main_queue(), ^{
        //get refrence of vertical indicator
        UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
        // get the relative position of the indicator in the main window
        //    CGPoint p = [verticalIndicator convertPoint:verticalIndicator.bounds.origin toView:[[UIApplication sharedApplication] keyWindow]];

        CGPoint p = CGPointMake(verticalIndicator.frame.origin.x-10,
                                verticalIndicator.frame.origin.y - scrollView.contentOffset.y);
        // set the custom view new position
        CGRect indicatorFrame = CGRectMake(verticalIndicator.frame.origin.x, verticalIndicator.frame.origin.y, 10, 10);
         self.indicatorView.frame = indicatorFrame;
       });

  }

还要确保您在视图中添加了 uiview 确实加载了方法.

Also maske sure you added the uiview in you view did load method.

//在viewDidLoad中:

请注意,我使用了指标视图位置的示例值.您可以根据需要替换这些值.

Note that I have used the sample values for the indicatorView postion.You can replace these as per your need.

indicatorView=[[UIView alloc]initWithFrame:CGRectMake( p.x, p.y , 10, 10)];

indicatorView.backgroundColor=[UIColor blueColor];

[self.tableView addSubview:indicatorView];

这篇关于跟随 UICollectionView 指示器的 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆