Search code examples
iosobjective-ccalayernan

SwipeView 'CALayer position contains NaN'


I'm trying to use SwipeView view, i'm making all like in paging example;

- (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;
}

But for some reason i having next issue: 'CALayer position contains NaN: [nan 227]'. On this line of code in SwipeView.m:

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

in - (void)setFrameForView:(UIView *)view atIndex:(NSInteger)index; method

After debugging and trying to find reason of this issue i saw that value of property in SwipeView scrollOffset never sets, and it's nil, then i'm setting _scrollOffset in my viewDidLoad method to 1, for example and it's works, but little messes up scrollOffset. In example of SwipeView everything works perfectly without setting this property to any value, any ideas why? Thanks!

PROJECT - github

Similar questions: - CALayer position contains NaN: [nan -0.5] - How to solve CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]?

EDIT:

The problem appears only when i'm pushing view with SwipeView.


Solution

  • are you adding items to the array for the _swipeView like the example?

    - (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)];
        }
    }
    

    I have never used SwipeView but It appears the offset is set from the views which are added. Thus this could be your problem.

    EDIT: I recreated your problem exactly, you are not connecting your datasource and delegate outlets in your xib or storyboard, control click and drag from your SwipeView to your ViewController and your problem should be fixed!