gpt4 book ai didi

ios - ios中collectionview滚动的动画

转载 作者:行者123 更新时间:2023-11-29 01:04:32 28 4
gpt4 key购买 nike

我有一个 Collection View ,因为我从网络服务加载了 10 张图像。我想将该 ImageView 从第一个位置自动滚动到最后一个位置,然后在该页面出现时连续持续到第一个位置。我使用下面的方法

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

我想在 Objective-C 中水平自动滚动。提前致谢

最佳答案

创建自定义 UICollectionViewCell 类,创建并连接 ImageView 的 socket 。

在 .h 文件中:

    NSMutableArray *imgArr;
NSInteger testIndexPath;

我想你可以实现 UICollectionView 的数据源方法。

在将图像添加到 ImageView 后,在 cellForItemAtIndexPath 中添加此行

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

现在将这两个方法添加到您的 .m 文件中:

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

关于ios - ios中collectionview滚动的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565290/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com