RGImageGallery
RGImageGallery 是一个可以显示图片或视频的 viewController,类似于幻灯片展示
- 过渡动画和系统“照片”应用中的交互手势
- 可定制的工具栏和显示视图
RGImageGallery 用于 RGImagePicker 来显示图片。以下是 RGImagePicker 的一些截图
安装
要将它添加到您的应用程序中,将两个类 RGImageGallery.h/.m
复制到您的 Xcode 项目中,或者通过添加以下内容到 Podfile 来通过 CocoaPods 添加
pod 'RGImageGallery'
用法
使用 DataSource 初始化。DataSource 提供图片数据。
- 数字
- 缩略图
- 大图
RGImageGallery *imageGallery = [[RGImageGallery alloc] initWithPlaceHolder:self.loadFailedImage andDataSource:self];
当数据源改变时被调用
- (void)updatePages:(NSIndexSet *_Nullable)pages;
- (void)insertPages:(NSIndexSet *_Nullable)pages;
- (void)deletePages:(NSIndexSet *_Nullable)pages;
RGImageGalleryPushTransitionDelegate
- 提供 Push 视图以进行 Push 或 Pop 过渡
- 处理将要 Push 事件
- 处理将要 Pop 事件
// Set push transition style
imageGallery.pushTransitionDelegate = self;
imageGallery.pushFromView = YES;
RGImageGalleryAdditionUIConfig
- 自定义工具栏
- 自定义视频播放按钮
- 自定义前端显示图片视图
RGImageGalleryDelegate
- 处理播放视频事件
- 处理停止视频事件
- 处理图片滑动事件
Push
- 通过点击 Push
[imageGallery showImageGalleryAtIndex:indexPath.row fatherViewController:self];
- 通过捏合手势 Push
捏合手势应添加到您的视图中,并自行控制进度。以下是一个示例。
- (void)pin:(UIPinchGestureRecognizer *)gesture {
switch (gesture.state) {
case UIGestureRecognizerStateBegan:{
NSIndexPath *path = nil;
NSUInteger touchCount = gesture.numberOfTouches;
if (touchCount == 2) {
CGPoint p1 = [gesture locationOfTouch:0 inView:self.collectionView];
CGPoint p2 = [gesture locationOfTouch:1 inView:self.collectionView];
CGPoint center = CGPointMake((p1.x+p2.x)/2,(p1.y+p2.y)/2);
path = [self.collectionView indexPathForItemAtPoint:center];
}
if (!path) {
return;
}
RGImageGallery *imageGallery = [[RGImageGallery alloc] initWithPlaceHolder:self.loadFailedImage andDataSource:self];
imageGallery.pushTransitionDelegate = self;
imageGallery.pushFromView = YES;
// record originSize
self.rg_originSize = [self.collectionView cellForItemAtIndexPath:path].frame.size;
// began interaction push
[imageGallery beganInteractionPushAtIndex:path.row fatherViewController:self];
self.imageGallery = imageGallery;
self.interactionAnimate = YES;
// do animate for interaction
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.1 delay:0 options:0 animations:^{
NSUInteger touchCount = gesture.numberOfTouches;
if (touchCount == 2) {
CGPoint p1 = [gesture locationOfTouch:0 inView:self.collectionView];
CGPoint p2 = [gesture locationOfTouch:1 inView:self.collectionView];
CGSize size = CGSizeMake(fabs(p1.x - p2.x), fabs(p1.y - p2.y));
CGPoint center = CGPointMake((p1.x + p2.x) / 2,(p1.y + p2.y) / 2);
center = [self.collectionView convertPoint:center toView:self.view];
// update interaction view center
[imageGallery updateInteractionPushCenter:center];
// update interaction view size
[imageGallery updateInteractionPushSize:size];
}
} completion:^(BOOL finished) {
self.interactionAnimate = NO;
// reset data and began gesture
gesture.scale = 1;
[self pin:gesture];
}];
});
break;
}
case UIGestureRecognizerStateChanged:{
// is animating, ignore change
if (self.interactionAnimate) {
return;
}
NSUInteger touchCount = gesture.numberOfTouches;
if (touchCount == 2) {
CGPoint p1 = [gesture locationOfTouch:0 inView:self.collectionView];
CGPoint p2 = [gesture locationOfTouch:1 inView:self.collectionView];
CGSize size = CGSizeMake(fabs(p1.x - p2.x), fabs(p1.y - p2.y));
CGPoint center = CGPointMake((p1.x + p2.x) / 2,(p1.y + p2.y) / 2);
center = [self.collectionView convertPoint:center toView:self.view];
[self.imageGallery updateInteractionPushCenter:center];
[self.imageGallery updateInteractionPushSize:size];
UIView *view = [self.imageGallery interactionPushView];
CGFloat progress = (view.frame.size.width - self.rg_originSize.width) / self.view.frame.size.width;
[self.imageGallery updateInteractionPushProgress:progress];
}
break;
}
case UIGestureRecognizerStatePossible:
case UIGestureRecognizerStateEnded:{
if (self.interactionAnimate) {
return;
}
UIView *view = [self.imageGallery interactionPushView];
CGFloat progress = [self.imageGallery interactionPushProgress];
if (!progress) {
progress = (view.frame.size.width - self.rg_originSize.width) / self.view.frame.size.width;
}
// finish interaction push, if gesture.scale >= 1, we think push result is succeed
[self.imageGallery finishInteractionPush:gesture.scale >= 1 progress:progress];
break;
}
case UIGestureRecognizerStateFailed:
case UIGestureRecognizerStateCancelled:
default:{
// push failed
[self.imageGallery finishInteractionPush:NO progress:[self.imageGallery interactionPushProgress]];
break;
}
}
}