swift 电子书阅读器

1. 效果图

                 

                 

2. 知识点

a) UITableViewController

b) AutoLayout,SizeClasses

c) UIPageViewController

d) load url image

e) Navigation Controller

f) Activity jumping using source code(presentViewController) or story board(prepareForSegue)


3. 代码

storyboard:



BookReaderPageViewController.swift:

//
//  BookReaderPageViewController.swift
//  bookReader
//
//  Created by luke li on 18/2/2016.
//  Copyright © 2016 luke li. All rights reserved.
//

import UIKit



class BookReaderPageViewController: UIPageViewController,UIPageViewControllerDataSource {

    //var pageHeadings=["girl1","girl2","food3"]
    //var pageImages=["girl1","girl2","http://fast.ishenman.com/z/只有我不在的街道/第01话/06.jpg"]
    //var pageContent=["girl1 label","girl2 label","food3 label"]
    var pages=[0,42,28,28,30,32,27,27,26,28,29,
                 34,34,32,31,31,30,28,28,29,34,
                 27,28,31,30,29,31,30,32,30,30,
                 40,45,26,32,50,41,38,31,33,38,
                 44,44]
    var chapter=0;
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //NSLog("%@,%@()",__FILE__,__FUNCTION__)
        // Do any additional setup after loading the view.
        // Set the data source to itself NSLog("BookReaderContentViewController")
        dataSource = self
        NSLog("segue parameter passed chapter=%d",chapter);
        // Create the first walkthrough screen
        if let startingViewController = viewControllerAtIndex(1) {
            setViewControllers([startingViewController], direction: .Forward, animated: true, completion: nil)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        
        var index = (viewController as! BookReaderContentViewController).index;
        index++;
        
        return viewControllerAtIndex(index);

    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        
        var index = (viewController as! BookReaderContentViewController).index;
        index--;
        
        return viewControllerAtIndex(index);
    }

    func viewControllerAtIndex(index:Int)->BookReaderContentViewController?
    {
        
        if index == NSNotFound || index <= 0 || index > pages[chapter] {
            return nil
        }
        
        if let pageContentViewController = storyboard?.instantiateViewControllerWithIdentifier("BookReaderContentViewController") as? BookReaderContentViewController {
            
            var strchapter:String=""
            var strindex:String=""
            
            if(chapter<10 && chapter != 6)
            {
                strchapter = "0\(chapter)"
            }
            else
            {
                strchapter = String(chapter)
            }
            
            if(index<10)
            {
                 strindex = "0\(index)"
            }
            else
            {
                 strindex = String(index)
            }
            
            if(2==chapter || 9==chapter
                || 13 == chapter || 29 == chapter || 31 == chapter
                || 36 == chapter)
            {
                strindex = "0" + strindex;
            }
            else if(6==chapter)
            {
                strindex = "%E7%AC%AC6%E8%AF%9D_00" + strindex;
            }
            else if(8 == chapter || 10==chapter)
            {
                strindex = "00"+strindex;
            }
            else if(14 == chapter || 16==chapter || 20 == chapter || 21 == chapter
                || 22 == chapter || 23 == chapter || 24 == chapter || 25 == chapter
                || 26 == chapter || 27 == chapter || 30 == chapter || 32 == chapter
                || 33 == chapter || 41 == chapter)
            {
                strindex = "ECYMD_0"+strindex;
            }
            else if(18==chapter)
            {
                strindex = "d54063_00"+strindex;
            }
            else if(28==chapter || 42 == chapter)
            {
                strindex = "pic_0"+strindex;
            }
         
            
         
            
            var url="http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/%E7%AC%AC"+strchapter+"%E8%AF%9D/"+strindex+".jpg";
            
            if(13 == chapter || 15 == chapter || 17 == chapter || 18 == chapter)
            {
            url = "http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/"+strchapter+"/"+strindex+".jpg"
            }
            else if(19 == chapter)
            {
            url = "http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/%E7%AC%AC"+strchapter+"%E8%AF%9D_1387691297/ECYMD_0"+strindex+".jpg"
            }
            else if(35 == chapter)
            {
            url = "http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/%E7%AC%AC"+strchapter+"%E8%AF%9D_1434198748/pic_0"+strindex+".jpg"
            }
            else if(38 == chapter)
            {
            url = "http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/%E7%AC%AC"+strchapter+"%E8%AF%9D_1442281494/pic_0"+strindex+".jpg"
            }
            else if(37 == chapter || 39 == chapter)
            {
            url = "http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/"+strchapter+"/ECYMD_0"+strindex+".jpg"
            }
            else if(40==chapter)
            {
            url = "http://images.ishenman.com/z/%E5%8F%AA%E6%9C%89%E6%88%91%E4%B8%8D%E5%9C%A8%E7%9A%84%E8%A1%97%E9%81%93/%E7%AC%AC"+strchapter+"%E8%AF%9D_1447737940/ECYMD_0"+strindex+".jpg"
            }
            
            NSLog("url:%@",url);
            
            pageContentViewController.imageFile = url;
            pageContentViewController.index = index
            pageContentViewController.pages = pages[chapter]
            pageContentViewController.chapter = chapter
            
            return pageContentViewController
        }
        
        return nil
    
    }
    
    func forward(index:Int) {
        
        if let nextViewController = viewControllerAtIndex(index + 1) {
            setViewControllers([nextViewController], direction: .Forward, animated: true, completion: nil)
        }
    }
    
   

 
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

BookReaderContentViewController:

//
//  BookReaderContentViewController.swift
//  bookReader
//
//  Created by luke li on 20/2/2016.
//  Copyright © 2016 luke li. All rights reserved.
//

import UIKit

class BookReaderContentViewController: UIViewController {

    @IBOutlet var contentImageview:UIImageView!
    @IBOutlet var contentLabel:UILabel!
    
    var imageFile=""
    var index=0;
    var pages=0;
    var chapter=0;

    override func viewDidLoad() {
        super.viewDidLoad()

        //NSLog("%@,%@()",__FILE__,__FUNCTION__)
        NSLog("chapter=%d,index=%d,page=%d",chapter,index,pages)
       
        if let url = NSURL(string: imageFile) {
            NSLog("url exist");
            if let data = NSData(contentsOfURL: url) {
                NSLog("url data exist");
                contentImageview.image = UIImage(data: data)
            }
            else
            {
               NSLog("url data not exist!");
            }
        }
        else
        {
            NSLog("url not exist!");
        }

        contentLabel.text = "第\(chapter)章   \(index)/\(pages)";
       // contentImageview.image = UIImage(named:imageFile)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        // Dispose of any resources that can be recreated.
    }
    


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

BookReaderTableViewController.swift:

//
//  BookReaderTableViewController.swift
//  bookReader
//
//  Created by luke li on 27/2/2016.
//  Copyright © 2016 luke li. All rights reserved.
//

import UIKit

class BookReaderTableViewController: UITableViewController {

    var chapterTotal=42
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)  //解决状态栏重叠问题
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return chapterTotal; //bookchapters.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! BookReaderTableViewCell
        
        
        // Configure the cell...
        cell.cellLabel.text = "第\(indexPath.row+1)章"; //bookchapters[indexPath.row]
        
        return cell
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showChapterDetail"
        {
            if let indexPath = tableView.indexPathForSelectedRow
            {
                let destinationController = segue.destinationViewController as! BookReaderPageViewController
                destinationController.chapter = indexPath.row+1;
            
            }
            
        }
    }
   /* luke del
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "只有我不存在的城市"
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       // if indexPath.row == 0
        NSLog("indexPATH.row=%d",indexPath.row);
        
        if let pageViewController = storyboard?.instantiateViewControllerWithIdentifier("BookReaderPageController") as? BookReaderPageViewController {
            
            pageViewController.chapter=indexPath.row+1;
            presentViewController(pageViewController, animated: true, completion: nil)
        }

    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

BookReaderTableViewCell.swift:

//
//  BookReaderTableViewCell.swift
//  bookReader
//
//  Created by luke li on 27/2/2016.
//  Copyright © 2016 luke li. All rights reserved.
//

import UIKit

class BookReaderTableViewCell: UITableViewCell {
    @IBOutlet weak var cellLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


Info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>en</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>$(PRODUCT_NAME)</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>1.0</string>
	<key>CFBundleSignature</key>
	<string>????</string>
	<key>CFBundleVersion</key>
	<string>1</string>
	<key>LSRequiresIPhoneOS</key>
	<true/>
	<key>UILaunchStoryboardName</key>
	<string>LaunchScreen</string>
	<key>UIMainStoryboardFile</key>
	<string>Main</string>
	<key>UIRequiredDeviceCapabilities</key>
	<array>
		<string>armv7</string>
	</array>
	<key>UISupportedInterfaceOrientations~ipad</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationPortraitUpsideDown</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
</dict>
</plist>


4.工程下载

http://download.csdn.net/detail/gaoguoxin2/9446384



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值