iOS一些小技巧 | 四

一,上传gif动图的时候,需要不解码的Data数据,需要通过路径获取转换成Data:

//
//上传gif图
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"motion" ofType:@"GIF"] ];

//以下方式转换上传的gif图没有动态效果
UIImagePNGRepresentation(image);
UIImageJPEGRepresentation(image, 1.0);
//

二,collectionview滑动固定其滑动距离,分页滑动:

//
//
@interface SkyVideoCoueseLayout : UICollectionViewFlowLayout
@property (nonatomic,assign)CGFloat dragShort;
@end
//
//
#import "SkyVideoCoueseLayout.h"

@implementation SkyVideoCoueseLayout


- (void)prepareLayout
{
    [super prepareLayout];
    if(self.dragShort <= 0){
        return;
    }
    if(self.collectionView != nil){
        self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
        CGSize size = [[self.collectionView valueForKey:@"interpageSpacing"] CGSizeValue];
        if(!CGSizeEqualToSize(CGSizeZero, size)){
            if(self.scrollDirection == UICollectionViewScrollDirectionHorizontal){
                NSValue *sizeValue = [NSValue valueWithCGSize:CGSizeMake(-self.dragShort, 0.0)];
                [self.collectionView setValue:sizeValue forKey:@"interpageSpacing"];
            }else{
                NSValue *sizeValue = [NSValue valueWithCGSize:CGSizeMake(0.0, -self.dragShort)];
                [self.collectionView setValue:sizeValue forKey:@"interpageSpacing"];
            }
        }
        CGPoint origin = [[self.collectionView valueForKey:@"pagingOrigin"] CGPointValue];
        if(!CGPointEqualToPoint(CGPointZero, origin)){
            NSValue *originValue = [NSValue valueWithCGPoint:CGPointZero];
            [self.collectionView setValue:originValue forKey:@"pagingOrigin"];
        }
    }
}

@end
//

三,collectionview cell出现的时候产生动画:

//
//代理
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
   // 这个方法会在 cell 即将显示在屏幕上时调用
   // 你可以在这里执行缩小动画

   // 假设你要为 cell 添加缩小动画
   if ([cell isKindOfClass:[UICollectionViewCell class]]) {
       UICollectionViewCell *cellToAnimate = (UICollectionViewCell *)cell;
       
       // 缩小为原始尺寸的 80%
       cellToAnimate.transform = CGAffineTransformMakeScale(0.8, 0.8);
       
       [UIView animateWithDuration:0.3 animations:^{
           // 恢复原始尺寸
           cellToAnimate.transform = CGAffineTransformIdentity;
       }];
   }
}
//
//

Leave a Reply

Your email address will not be published. Required fields are marked *