A
A
anonymous222013-11-29 17:44:28
iPhone
anonymous22, 2013-11-29 17:44:28

SwipeView problem

I'm trying to understand SwipeView , there was a problem when using it in my project, I use the following code from the example (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;
}

The application crashes with an error: CALayer position contains NaN: [nan 284]and points to a line with
view.center = CGPointMake(center.x, _scrollView.frame.size.height/2.0f);
.
Whole method:
- (void)setFrameForView:(UIView *)view atIndex:(NSInteger)index
{
    if (self.window)
    {
        CGPoint center = view.center;
        
        if (_vertical)
        {
            center.y = ([self offsetForItemAtIndex:index] + 0.5f) * _itemSize.height + _scrollView.contentOffset.y;
        }
        else
        {
            center.x = ([self offsetForItemAtIndex:index] + 0.5f) * _itemSize.width + _scrollView.contentOffset.x;// center.x NAN
        }
        
        BOOL disableAnimation = !CGPointEqualToPoint(center, view.center);
        BOOL animationEnabled = [UIView areAnimationsEnabled];
        if (disableAnimation && animationEnabled) [UIView setAnimationsEnabled:NO];
        
        if (_vertical)
        {
            view.center = CGPointMake(_scrollView.frame.size.width/2.0f, center.y);
        }
        else
        {
            view.center = CGPointMake(center.x, _scrollView.frame.size.height/2.0f);
        }
        
        view.bounds = CGRectMake(0.0f, 0.0f, _itemSize.width, _itemSize.height);
        
        if (disableAnimation && animationEnabled) [UIView setAnimationsEnabled:YES];
    }
}

When using breakpoints, it shows that center.x becomes NAN at some point (I indicated where in the comment). I can not understand what the problem is, I seem to do everything as in the example. Please help me figure out what to do.
EDIT:
Found the cause of the problem: it is due to the fact that there is a Navigation segue from the initial ViewControllera in the VC with SwipeView.
Github

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
anonymous22, 2013-11-30
@anonymous22

I'm trying to figure out why this is happening, but one solution to this problem is to add [self.view addSubview:_swipeView] to your viewDidLoad method. I think that the problem is with the life cycle and in what sequence the loading occurs in the swipeView itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question