2016-04-12 7 views
0

Ich habe eine Sammlung in dem ich 10 Bilder von Webservice geladen habe. Ich möchte diese Bildansicht von der ersten zur letzten Position automatisch scrollen und dann dauernd zur ersten Position dauern, wenn diese Seite erscheint. Ich benutze unten MethodeAnimation des Scrollens in Sammlungsansicht in IOS

-(void)scrollSlowly 
{ 
    CATransition *transition = [CATransition animation]; 
    transition.duration = 4.0f; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromLeft; 
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
    [UIView animateWithDuration:5.0f 
          delay:3.0f 
         options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse 
        animations:^{ 

         [self.CollectionView setContentOffset:CGPointMake(CGFLOAT_MAX ,0)]; 

        } 
        completion:nil]; 
    [self.CollectionView.layer addAnimation:transition forKey:@"transition"]; 
} 

-(void)scrollSlowlyToPoint 
{ 
    self.CollectionView.contentOffset = self.scrollingPoint; 
    // Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint. 
    if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint)) 
    { 
     [self.scrollingTimer invalidate]; 
    } 
    // Going one pixel to the right. 
    self.scrollingPoint = CGPointMake(self.scrollingPoint.x+1, self.scrollingPoint.y); 
} 

I horizontal in Objective-C zu Autoscroll wollen. Vielen Dank im Voraus

+2

Warum würden Sie nicht verwenden '- (void) scrollToItemAtIndexPath: (NSIndexPath *) indexPath atScrollPosition: (UICollectionViewScrollPosition) scroll animiert: (BOOL) animiert? – s0urcer

+0

Ja, ich benutze das auch, aber es wird mir nicht repetitive langsam scrollen ... @sourcer – Aashi

Antwort

1

Erstellen Sie eine benutzerdefinierte UICollectionViewCell-Klasse, erstellen und verbinden Sie eine Steckdose für die Bildansicht.

In .h-Datei:

NSMutableArray *imgArr; 
    NSInteger testIndexPath; 

Ich denke, Sie die Datenquelle Methoden für UICollectionView umsetzen können.

In cellForItemAtIndexPath diese Zeile hinzufügen, nachdem Bilder auf den Imageview

testIndexPath=indexPath.row;//this will give you the indexPath for cell 

Nun fügen Sie diese beiden Methoden in .m-Datei hinzufügen:

-(void)autoScroll{ 
    CGFloat point = self.collectionVw.contentOffset.x; 
    CGFloat lo = point + 1; 
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
    self.collectionVw.contentOffset = CGPointMake(lo, 0); 
}completion:^(BOOL finished){ 
    if (testIndexPath==8) 
    { 
     [self autoScrollReverse]; 
    } 
    else{ 
     [self autoScroll]; 
    } 
    }]; 
} 

    -(void)autoScrollReverse{ 
    CGFloat point = self.collectionVw.contentOffset.x; 
    CGFloat lo = point - 1; 
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
    self.collectionVw.contentOffset = CGPointMake(lo, 0); 
}completion:^(BOOL finished){ 
    if(testIndexPath == 0){ 
     [self autoScroll]; 
    }else{ 
     [self autoScrollReverse]; 

    } 

    }]; 
} 

//Call [self autoScroll] in your viewDidLoad 
+0

Ich habe meine Bildansicht in der Zelle von collectionview .... Wie konnte ich kontinuierliche scrollende Animation horizontal durch obigen Code erreichen ... konnte es nicht bekommen .. @ Akash KR – Aashi

+0

meine Entschuldigung ... Bearbeitung meines Codes –

+0

folgen Sie diesem: http://StackOverflow.com/Questions/13006972/Creating-Slow-Scrolling-In-IndexPath-in-Uicollectionview –

0

bessere Art und Weise zu bewegen Index von oben nach unten und unten nach oben.

Diese indizieren bewegen 10 Mittel scrollen oben nach unten

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:9 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES]; 

Für wieder unten nach oben scrollen, die zum Index bewegen 0

[UIView animateWithDuration:3 
         delay:0.1 
        options:UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES]; 
       } 
       completion:nil]; 

Sie können auch festlegen, von oben nach unten Scrollen in UIVIew Animation wie oben. Legen Sie die erste Methode in ViewWillAppear fest und legen Sie die zweite Methode in ViewDidAppear fest, wenn Sie für jede angezeigte Seite eine Animation benötigen

0

Bitte wenden Sie diese an. dessen Hilfe Sie können

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath]; 

    [UIView animateWithDuration:0.2 animations:^{ 
    cell.transform = CGAffineTransformMakeTranslation(0.0, -50); 
    }]; 
    [self delayBy:0.5 code:^{ // call block of code after time interval 
    [UIView animateWithDuration:0.3 animations:^{ 
     cell.transform = CGAffineTransformIdentity; 
    }]; 
    }]; 
    return cell; 
} 
+0

[self delay by:]; Diese Methode fehlt Ihnen können Sie die Verzögerung durch Methode hinzufügen .. Danke –

0

sein Dieses Ihr Problem lösen helfen können:

int maxDelay = 4; 
int delayInSeconds = arc4random() % maxDelay; 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    //your codes// 
}); 
Verwandte Themen