【问题标题】:SwipeView 'CALayer position contains NaN'SwipeView 'CALayer 位置包含 NaN'
【发布时间】:2013-11-29 13:41:37
【问题描述】:

我正在尝试使用SwipeView 视图,我正在像paging 示例中一样;

- (void)viewDidLoad{
    [super viewDidLoad];

    _swipeView.pagingEnabled = YES;
}

- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView{
    return [_items count];
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{

    //create new view if no view is available for recycling
    if (view == nil)
    {
        //don't do anything specific to the index within
        //this `if (view == nil) {...}` statement because the view will be
        //recycled and used with other index values later
        view = [[UIView alloc] initWithFrame:self.swipeView.bounds];
        view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    }


    //set background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    view.backgroundColor = [UIColor colorWithRed:red
                                           green:green
                                            blue:blue
                                           alpha:1.0];

    return view;
}

- (CGSize)swipeViewItemSize:(SwipeView *)swipeView
{
    return self.swipeView.bounds.size;
}

但由于某种原因,我遇到了下一个问题:'CALayer position contains NaN: [nan 227]'。 在 SwipeView.m 中的这行代码:

view.center = CGPointMake(center.x, _scrollView.frame.size.height/2.0f);

- (void)setFrameForView:(UIView *)view atIndex:(NSInteger)index;方法中

在调试并尝试找到此问题的原因后,我看到 SwipeView scrollOffset 中的属性值从未设置,并且为零,然后我将 viewDidLoad 方法中的 _scrollOffset 设置为 1,例如,它有效,但很少弄乱了滚动偏移。在 SwipeView 的示例中,无需将此属性设置为任何值,一切都可以正常工作,有什么想法吗?谢谢!

项目 - github

类似的问题: - CALayer position contains NaN: [nan -0.5] - How to solve CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]?

编辑:

只有当我使用 SwipeView 推送视图时才会出现问题。

【问题讨论】:

    标签: ios objective-c calayer nan


    【解决方案1】:

    您是否像示例一样将项目添加到 _swipeView 的数组中?

    - (void)awakeFromNib
    {
        //set up data
        //your swipeView should always be driven by an array of
        //data of some kind - don't store data in your item views
        //or the recycling mechanism will destroy your data once
        //your item views move off-screen
        self.items = [NSMutableArray array];
        for (int i = 0; i < 100; i++)
        {
            [_items addObject:@(i)];
        }
    }
    

    我从未使用过 SwipeView,但似乎偏移量是从添加的视图中设置的。因此,这可能是您的问题。

    编辑: 我完全重现了你的问题,你没有在你的 xib 或故事板中连接你的数据源和委托出口,控制点击并从你的 SwipeView 拖动到你的 ViewController,你的问题应该得到解决!

    【讨论】:

    • 您是否在您的 xib 或情节提要中连接了您的代理和数据源插座?
    • 我试图弄清楚为什么会这样,但是如果您将 [self.view addSubview:_swipeView] 添加到您的 viewDidLoad 中,它会解决所有问题,我怀疑它与视图生命周期和定义和加载每个帧等的顺序,以及它在 swipeView 中的使用方式。
    • 实际上这并没有解决问题,在我尝试向此 swipeView 添加一些自定义视图并运行应用程序后,它崩溃并出现错误:-[SwipeView layoutSublayersOfLayer:] 中的断言失败,执行 -layoutSubviews 后仍需要自动布局。 SwipeView对-layoutSubviews的实现需要调用super。
    • 添加后[super layoutSubviews];到滑动视图中的 layoutSubviews 方法结束,它可以解决问题,我想...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多