如何为图像制作图像页面查看器 swift ios
How to make Image page viewer for images swift ios
我想在我的应用程序中显示资产文件夹中的一些图像。所以我想做一个页面浏览量。
首先,图像将在 collection 视图中,然后单击时,图像将全屏显示。然后用户可以通过左右滑动在图像之间滑动,如下图所示:
我找到了这个教程:
更新:
我的故事板中有这个:
现在 GaleryViewController
我在单元格中显示图像
当用户点击它时,我在 PhotoViewController
中全屏打开图像。
PhotoViewController.swift :
import UIKit
class PhotoViewController: UIViewController{
@IBOutlet weak var imageView: UIImageView!
var index: Int = 0;
var pageViewController : UIPageViewController?
@IBAction func btnCancelClicked(sender: AnyObject) {
self.navigationController?.popToRootViewControllerAnimated(true);
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
initUI();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
self.navigationController?.hidesBarsOnTap = true;
self.navigationController?.hidesBarsOnSwipe = true;
displayPhoto()
}
func initUI() -> Void {
// pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
// pageViewController!.dataSource = self
}
func displayPhoto() {
self.imageView.image = UIImage(named: Constants.Statics.images[index])
}
我有静态结构的图像,所以我可以在任何地方访问它们:
class Constants {
struct Statics {
static let images = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","7.jpg","8.jpg"]
}
}
我的问题是:我想添加允许我在 PhotoViewController
.
中的图片之间滑动的功能
有什么想法吗?
谢谢
您可以执行以下两项操作之一来实现您的目标:
- 将单元格模式转换到下一个
UICollectionViewController
图像全屏显示的位置,并在 prepareForSegue
中传递您需要显示的所有数据(图像)以及显示位置正是你在这一刻 (indexOfImage
).
- 您可以观看
didSelectItemAtIndexPath
,然后将所有数据传递给您想要使用 presentViewController
函数显示的下一个 UICollectionViewController
的一个实例。
但非常重要的是,在接下来的 UICollectionViewController
中,您已经在代码或 Interface Builder 中设置了 pageEnabled = true
(虽然我很容易做到。)
UPDATE:
关于如何使用 UIScrollView
或 UICollectionView
制作图像幻灯片的非常好的教程:
- How To Use UIScrollView to Scroll and Zoom Content
- Creating a Paged Photo Gallery With a UICollectionView
希望对你有所帮助。
我想在我的应用程序中显示资产文件夹中的一些图像。所以我想做一个页面浏览量。
首先,图像将在 collection 视图中,然后单击时,图像将全屏显示。然后用户可以通过左右滑动在图像之间滑动,如下图所示:
我找到了这个教程:
更新:
我的故事板中有这个:
现在 GaleryViewController
我在单元格中显示图像
当用户点击它时,我在 PhotoViewController
中全屏打开图像。
PhotoViewController.swift :
import UIKit
class PhotoViewController: UIViewController{
@IBOutlet weak var imageView: UIImageView!
var index: Int = 0;
var pageViewController : UIPageViewController?
@IBAction func btnCancelClicked(sender: AnyObject) {
self.navigationController?.popToRootViewControllerAnimated(true);
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
initUI();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
self.navigationController?.hidesBarsOnTap = true;
self.navigationController?.hidesBarsOnSwipe = true;
displayPhoto()
}
func initUI() -> Void {
// pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
// pageViewController!.dataSource = self
}
func displayPhoto() {
self.imageView.image = UIImage(named: Constants.Statics.images[index])
}
我有静态结构的图像,所以我可以在任何地方访问它们:
class Constants {
struct Statics {
static let images = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","7.jpg","8.jpg"]
}
}
我的问题是:我想添加允许我在 PhotoViewController
.
有什么想法吗?
谢谢
您可以执行以下两项操作之一来实现您的目标:
- 将单元格模式转换到下一个
UICollectionViewController
图像全屏显示的位置,并在prepareForSegue
中传递您需要显示的所有数据(图像)以及显示位置正是你在这一刻 (indexOfImage
). - 您可以观看
didSelectItemAtIndexPath
,然后将所有数据传递给您想要使用presentViewController
函数显示的下一个UICollectionViewController
的一个实例。
但非常重要的是,在接下来的 UICollectionViewController
中,您已经在代码或 Interface Builder 中设置了 pageEnabled = true
(虽然我很容易做到。)
UPDATE:
关于如何使用 UIScrollView
或 UICollectionView
制作图像幻灯片的非常好的教程:
- How To Use UIScrollView to Scroll and Zoom Content
- Creating a Paged Photo Gallery With a UICollectionView
希望对你有所帮助。