zoukankan      html  css  js  c++  java
  • 电子书阅读器开发 (一)

    从今天开始陪大家一起开发一款电子书阅读器,主要是想帮助初学者快速入门ios。

     首先解决两个问题:1.对txt文件分页 2.实现翻页的效果

     1 -(NSString *)readStrFromTxt:(NSString *)fileName
     2 {
     3     NSError *error;
     4     
     5     NSString *textFileContents = [NSString
     6                                   
     7                                   stringWithContentsOfFile:[[NSBundle mainBundle]
     8                                                             
     9                                                             pathForResource:fileName
    10                                                             
    11                                                             ofType:@"txt"]
    12                                   
    13                                   encoding:NSUTF8StringEncoding
    14                                   
    15                                   error: & error];
    16     
    17     // If there are no results, something went wrong
    18     
    19     if (textFileContents == nil) {
    20         
    21         // an error occurred
    22         
    23         NSLog(@"Error reading text file. %@", [error localizedFailureReason]);
    24         
    25     }
    26     
    27     return textFileContents;
    28 }

    读取txt文本文件,并且转化为字符串

    1 bRect=CGRectMake(BOOK_LEFT, BOOK_TOP, BOOK_WIDTH, BOOK_HEIGHT);
    2 allStr=[self readStrFromTxt:@"aizaixianjingderizi"];
    3 NSArray *arrContent=[allStr componentsSeparatedByString:@"  "];
    4 NSString *curCotentLine=@"";
    5 for (int i=0; i<10; i++) {
    6    NSString *tempStr=[arrContent objectAtIndex:i];
    7    NSLog(@"tempStr.length:%d",tempStr.length);
    8    curCotentLine=[NSString stringWithFormat:@"%@%@",curCotentLine,tempStr];
    9 }

    分行显示txt文件的内容

    分页显示我们使用pageconroller这个控件

     1 // 设置UIPageViewController的配置项
     2     NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
     3                                                        forKey: UIPageViewControllerOptionSpineLocationKey];
     4     // 实例化UIPageViewController对象,根据给定的属性
     5     _pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
     6                                                           navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
     7                                                                         options: options];
     8     // 设置UIPageViewController对象的代理
     9     _pageController.dataSource = self;
    10     
    11     
    12     // 定义“这本书”的尺寸
    13     [[_pageController view] setFrame:[[self view] bounds]];
    14 
    15     
    16     // 让UIPageViewController对象,显示相应的页数据。
    17     // UIPageViewController对象要显示的页数据封装成为一个NSArray。
    18     // 因为我们定义UIPageViewController对象显示样式为显示一页(options参数指定)。
    19     // 如果要显示2页,NSArray中,应该有2个相应页数据。
    20     initialViewController =[[UIViewController alloc]init];// 得到第一页
    21     UITextView *txtView=[[UITextView alloc]initWithFrame:bRect];
    22     txtView.text=curCotentLine;
    23     [initialViewController.view addSubview:txtView];
    24     
    25     NSArray *viewControllers =[NSArray arrayWithObject:initialViewController];
    26     [_pageController setViewControllers:viewControllers
    27                               direction:UIPageViewControllerNavigationDirectionForward
    28                                animated:NO
    29                              completion:nil];
    30     
    31     // 在页面上,显示UIPageViewController对象的View
    32     [self addChildViewController:_pageController];
    33     [[self view] addSubview:[_pageController view]];

    下面是pageconroller里面的委托函数

     1 #pragma mark- UIPageViewControllerDataSource
     2 
     3 // 返回上一个ViewController对象
     4 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
     5     
     6     UIViewController *beforeControl=[[UIViewController alloc]init];
     7     
     8     if(curIndex>WORD_COUNT)
     9     {
    10         curPage--;
    11         
    12         curIndex-=WORD_COUNT;
    13 
    14         UITextView *beforeTxt=[[UITextView alloc]initWithFrame:bRect];
    15         NSString *beforeStr=[allStr substringWithRange:NSMakeRange(curIndex, WORD_COUNT)];
    16         beforeTxt.text=beforeStr;
    17         
    18         NSArray *arrContent=[allStr componentsSeparatedByString:@"  "];
    19         NSString *curCotentLine=@"";
    20         for (int i=0; i<10; i++) {
    21             NSString *tempStr=[arrContent objectAtIndex:i+curPage*10];
    22             curCotentLine=[NSString stringWithFormat:@"%@%@",curCotentLine,tempStr];
    23         }
    24         beforeTxt.text=curCotentLine;
    25         
    26         NSLog(@"beforeStr:%@",beforeStr);
    27         
    28         [beforeControl.view addSubview:beforeTxt];
    29     }
    30     
    31     NSLog(@"curindex:%d",curIndex);
    32     
    33     return beforeControl;
    34 }
    35 
    36 // 返回下一个ViewController对象
    37 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    38     
    39     UIViewController *afterConrol=[[UIViewController alloc]init];
    40     
    41     if(curIndex+WORD_COUNT<allStr.length)
    42     {
    43         curPage++;
    44         
    45         UITextView *afterTxt=[[UITextView alloc]initWithFrame:bRect];
    46         NSString *afterStr=[allStr substringWithRange:NSMakeRange(curIndex,WORD_COUNT)];
    47         afterTxt.text=afterStr;
    48         
    49         NSLog(@"afterStr:%d",afterStr.length);
    50         
    51         NSArray *arrContent=[allStr componentsSeparatedByString:@"  "];
    52         NSString *curCotentLine=@"";
    53         for (int i=0; i<10; i++) {
    54             NSString *tempStr=[arrContent objectAtIndex:i+curPage*10];
    55             curCotentLine=[NSString stringWithFormat:@"%@%@",curCotentLine,tempStr];
    56         }
    57         afterTxt.text=curCotentLine;
    58         
    59         [afterConrol.view addSubview:afterTxt];
    60         
    61         curIndex+=WORD_COUNT;
    62     }
    63     
    64     NSLog(@"curindex:%d",curIndex);
    65     
    66     return afterConrol;
    67 }

    分别是翻页时候的显示效果。。。。今天就写到这了。。。。。未完待续。。。。。。下面附上效果截图。。。。。。

  • 相关阅读:
    pytorch 文本输入处理
    理解 on-policy 和 off-policy
    Monte Carlo与TD算法
    Monte Calro Tree Search (MCTS)
    强化学习概览
    linux 服务器 keras 深度学习环境搭建
    sed和awk学习整理
    linux shell编程进阶学习(转)
    gdb调试:
    MySQL C API 访问 MySQL 示例
  • 原文地址:https://www.cnblogs.com/guchengfengyun/p/4058991.html
Copyright © 2011-2022 走看看