ios UIKit框架分析 第4天

1.在iOS 5.0及以后,iOS为UIViewController类添加了新的属性和方法:

@property(nonatomic,readonly) NSArray *childViewControllers

- (void)addChildViewController:(UIViewController *)childController

- (void) removeFromParentViewController

- (void)willMoveToParentViewController:(UIViewController *)parent

- (void)didMoveToParentViewController:(UIViewController *)parent

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);

交换两个子视图控制器的位置(由于添加的顺序不同,所以子试图控制器在父视图控制器中存在层次关系)fromViewController:当前显示的子试图控制器,将被替换为非显示状态

toViewController:将要显示的子视图控制器

duration:交换动画持续的时间,单位秒

options:动画的方式

animations:动画Block

completion:完成后执行的Block

详细描述请参考:http://blog.sina.com.cn/s/blog_7b9d64af0101d6p7.html

2.UIPageViewController 属性方法简单介绍

typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {

    UIPageViewControllerNavigationOrientationHorizontal = 0,  水平方向翻页

    UIPageViewControllerNavigationOrientationVertical = 1   垂直方向翻页

};

typedef NS_ENUM(NSInteger, UIPageViewControllerSpineLocation) {  书脊在屏幕中的位置

    UIPageViewControllerSpineLocationNone = 0, // Returned if 'spineLocation' is queried when 'transitionStyle' is not 'UIPageViewControllerTransitionStylePageCurl'.  scroll类型时没有书脊

    UIPageViewControllerSpineLocationMin = 1// Requires one view controller.书脊在屏幕左端

    UIPageViewControllerSpineLocationMid = 2// Requires two view controllers.书脊在屏幕中间,需要2个内容控制器

    UIPageViewControllerSpineLocationMax = 3  // Requires one view controller.书脊在屏幕右端

};   // Only pertains to 'UIPageViewControllerTransitionStylePageCurl'.


typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationDirection) {翻页的方向 左-右 低端-顶端

    UIPageViewControllerNavigationDirectionForward,

    UIPageViewControllerNavigationDirectionReverse

};  // For 'UIPageViewControllerNavigationOrientationHorizontal', 'forward' is right-to-left, like pages in a book. For 'UIPageViewControllerNavigationOrientationVertical', bottom-to-top, like pages in a wall calendar.


typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {

    UIPageViewControllerTransitionStylePageCurl = 0, // Navigate between views via a page curl transition.书页翻页效果

    UIPageViewControllerTransitionStyleScroll = 1// Navigate between views by scrolling.滚动效果

};


UIKIT_EXTERN NSString *const UIPageViewControllerOptionSpineLocationKey;  UIPageViewControllerTransitionStylePageCurl时有效


UIKIT_EXTERN NSString *const UIPageViewControllerOptionInterPageSpacingKeyNS_AVAILABLE_IOS(6_0);UIPageViewControllerTransitionStyleScroll 时有效


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createContentPages];
    
    NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationNone] forKey:UIPageViewControllerOptionInterPageSpacingKey];
    self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:options];
    pageController.dataSource = self;
    [pageController.view setFrame:self.view.bounds];
    ContentViewController * initialViewController = [self viewCintrollerAtIndex:0];
    ContentViewController * endViewController = [self viewCintrollerAtIndex:1];

    NSArray * viewControllers = [NSArray arrayWithObjects:initialViewController,nil];
    [pageController setViewControllers:viewControllers direction:(UIPageViewControllerNavigationDirectionForward) animated:NO completion:nil];
    [self addChildViewController:pageController];
    [self.view addSubview:pageController.view];
    [pageController didMoveToParentViewController:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)createContentPages{
    NSMutableArray * pageStrings = [[NSMutableArray alloc]init];
    for (int i = 1; i < 11; i ++) {
        NSString * contentString = [[NSString alloc]initWithFormat:@"<html><head></head><body><h1>Chapter %d</h1><p>This is the page %d of content displayed using UIPageViewController in iOS 5.</p></body></html>",i,i];
        [pageStrings addObject:contentString];
    }
    pageContent = [[NSArray alloc]initWithArray:pageStrings];
}
- (ContentViewController *)viewCintrollerAtIndex:(NSUInteger)index{
    if ([self.pageContent count] == 0 || (index >= [self.pageContent count])) {
        return nil;
    }
    ContentViewController * dataViewController = [[ContentViewController alloc]initWithNibName:@"ContentViewController" bundle:nil];
    dataViewController.dataObject = [self.pageContent objectAtIndex:index];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    [dataViewController.view addGestureRecognizer:tap];
    
    return dataViewController;
}

- (NSUInteger)indexOfViewController:(ContentViewController*)viewController{
    return  [self.pageContent indexOfObject:viewController.dataObject];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.pageContent count]) {
        return nil;
    }
    return [self viewCintrollerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == 0 || (index == NSNotFound)) {
        return nil;
    }
    index --;
    return [self viewCintrollerAtIndex:index];
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return 10;
    
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}

详情请参考http://blog.sina.com.cn/s/blog_7b9d64af0101c2zg.html


3.UIPasteboard 浅析



4.UIPickerView 使用


5. UIPopoverController 浅析

->uipopoverController 只能用在ipad上,在iphone上实现这种弹出效果,封装一个类库可以实现,可以参考demo中用到的类库 FPPopover。

-> UIPopoverBackgroundView

UIPopoverBackgroundView 作为一个抽象基类必须被被继承然后重写一下几个方法:

+ (CGFloat)arrowHeight;
+ (CGFloat)arrowBase;
+ (UIEdgeInsets)contentViewInsets;        

/* This method may be overridden to prevent the drawing of the content inset and drop shadow inside the popover. The default implementation of this method returns YES.
 */
+ (BOOL)wantsDefaultContentAppearance NS_AVAILABLE_IOS(6_0);

popController.popoverBackgroundViewClass = [PopoverBackgroundView class];


6.print相关类 

#import <UIKit/UIPrintError.h>输出错误处理

#import <UIKit/UIPrintFormatter.h>输出格式化

#import <UIKit/UIPrintInfo.h>打印信息

#import <UIKit/UIPrintInteractionController.h>可以把你想打印的内容在ios打印了

#import <UIKit/UIPrintPageRenderer.h>html转化为PDF

#import <UIKit/UIPrintPaper.h>打印处理


-》UIPrintFormatter 

UIPrintFormatter时打印格式化的抽象基类: 展示 了传统的可打印的内容对象可以跨页边界。由于打印格式化,打印系统,可以自动打印与打印格式化的内容相关联的类型。以下为UIPrintFormatter的子类:


-》UISimpleTextPrintFormatter

UISimpleTextPrintFormatter类的实例进行布局打印纯文本,可能是多个页面。类允许您指定的印刷文字全球的字体,颜色,对齐线,和换行符的模式属性。


-》UIMarkupTextPrintFormatter

UIMarkupTextPrintFormatter类的实例展示了一个多页打印作业的HTML标记文本。


-》UIViewPrintFormatter

UIViewPrintFormatter类的一个实例,勾画出用于打印的视图绘制的内容。视图的内容可以跨越多个页面。


-》UIPrintInfo

一个UIPrintInfo对象封装了有关打印作业的信息,包括打印机标识,作业名称,输出类型(照片,正常,灰阶),方向(纵向或横向),和任何选定的双工模式,。打印系统打印时,使用此信息。


-》UIPrintInteractionController

UIPrintInteractionController类的共享实例,提出了印刷的用户界面和管理的文件,图像和其他可打印的内容在iOS打印。UIPrintInteractionController是IOS中央印刷类。它的共享实例代表一个打印作业。打印作业,包括其印刷相关的信息和选项,如输出类型,作业名称,纸张大小和方向,要打印的内容。


-》UIPrintPageRenderer

一个UIPrintPageRenderer对象绘制要打印页的内容带或不带打印格式化.A页面的渲染就是UIPrintPageRenderer自定义子类的一个实例。当您撰写打印作业使用的UIPrintInteractionController共享实例,您指定的页面渲染到该实例的printPageRenderer属性。


-》UIPrintPaper

UIPrintPaper类的一个实例封装使用的纸张尺寸,打印作业,并在其中的内容可以打印的矩形。

原文出处 http://hi.baidu.com/caoruifang/item/a2aade0837899f294bc4a357



7.UIReferenceLibraryViewController

-》用ios自带的字典查询单词

NSString *str = @"apple";
    if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:str] == YES)
    {
        UIReferenceLibraryViewController *reference =
        [[[UIReferenceLibraryViewController alloc] initWithTerm:str] autorelease];
        
        // Change the transition style
        [reference setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        
        [self presentModalViewController:reference animated:YES];
    }


8.UIRefreshControl

->UITableViewController已经内置了UIRefreshControl控件,该控件暂时只能用于UITableviewController,实现下拉刷新的功能。
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"Popover Title";
    
    self.refreshControl = [[UIRefreshControl alloc]init];
    [self.refreshControl setTintColor:[UIColor blueColor]];
    
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [self.refreshControl addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];
}

-(void)refreshAction
{
    [self.refreshControl endRefreshing];
}

9.UIResponder 详解

-》UIResponder Class Reference

Managing the Responder Chain

[plain]  view plain copy
  1. 1.- (UIResponder *)nextResponder  
返回接收者的下一个相应,如果没有就返回nil

UIResponder类不自动存储和设置下一个响应者,而是默认返回nil。子类必须override这个方法来设置下一个响应者。

UIView实现了这个方法,因为可以返回管理这个UIView的UIViewController或者它的父类;

UIViewController实现了这个方法,返回UIViewController的View的父View;

UIWindow发挥UIApplication对象;

UIApplication返回nil

[plain]  view plain copy
  1. 2.- (BOOL)isFirstResponder  
判断一个对象是否是第一响应者。
[plain]  view plain copy
  1. 3.- (BOOL)canBecomeFirstResponder  
判断一个对象是否可以成为第一响应者。默认返回NO。

如果一个响应对象通过这个方法返回YES,那么它成为了第一响应对象,并且可以接收触摸事件和动作消息。

子类必须overrider这个方法才可以成为第一响应者。

You must not send this message to a view that is not currently attached to the view hierarchy. The result is undefined.

[plain]  view plain copy
  1. 3.- (BOOL)becomeFirstResponder  
如果接收者接受了第一响应者的状态就返回YES,拒绝了这个状态就返回NO。默认返回YES。

子类可以override这个方法来更新状态或者执行一些行为,比如高亮选中项。

一个响应对象只有当前响应者可以放弃第一响应者状态,并且新的响应者可以成为第一响应者,才能成为第一响应对象。

[plain]  view plain copy
  1. 4.- (BOOL)canResignFirstResponder  
如果一个对象可以放弃对象响应者就返回YES。默认返回YES。

[plain]  view plain copy
  1. 5.- (BOOL)resignFirstResponder  
默认实现返回YES,放弃第一响应状态。子类可以override这个方法来更新状态或者执行一些行为,比如取消高亮选中项。

如果返回NO,拒绝放弃第一响应状态。

如果你override这个方法,必须调用父类的实现[super resignFirstResponder].

Managing Input Views

[plain]  view plain copy
  1. 1.@property (readonly, retain) UIView *inputView  
当一个对象变成第一响应者的时候显示的View

This property is typically used to replace the system-supplied keyboard that is presented for UITextField and UITextView objects.

UITextField和UITextView如果设置了inputView那么在becomeFirstResponder时不会显示键盘,而现实自定义的inputView;如果设置了inputAccessoryView那么在becomeFirstResponder时会在键盘的顶端显示自定义的inputAccessoryView。

[plain]  view plain copy
  1. 2.@property (readonly, retain) UIView *inputAccessoryView  

This property is typically used to attach an accessory view to the system-supplied keyboard that is presented for UITextField and UITextViewobjects.

[plain]  view plain copy
  1. 3.- (void)reloadInputViews  
当对象成为第一响应者的时候更新inputView和accessoryView。

Responding to Touch Events

[plain]  view plain copy
  1. 1.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
通知接收者当一个或多个手指在UIView或UIWindow上点下了。
将消息转发给下一个响应者,将消息发送给父类,不要将消息直接传递给下一个响应者。

如果你override这个方法而没有调用super..,你必须同样override其它响应触摸事件的方法,你要是空实现就好。

默认是不支持多点触摸的,如果想要响应多点触摸,你只要吧UIView的 multipleTouchEnabled 属性设置为YES即可。

[plain]  view plain copy
  1. 2.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
手指移动
[plain]  view plain copy
  1. 3.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
手指抬起
[plain]  view plain copy
  1. 4.- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
当收到一个系统干扰需要取消触摸事件时才会调用该方法,这种系统干扰往往会引起应用程序长时间没有响应或者一个View从window上移除了。
当收到touchesCancelled:withEvent:消息的时候需要清除所有通过touchesBegan:withEvent:创建的内容。

Responding to Motion Events

[plain]  view plain copy
  1. 1.- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event  
通知接收者一个动作开始了。
当一个动作开始了和结束了的时候iOS才会通知接收者。it doesn’t report individual shakes. 接收者必须是接收动作事件的第一响应者。
[plain]  view plain copy
  1. 2.- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event  
通知接收者一个动作结束了。
[plain]  view plain copy
  1. 3.- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event  
一个动作被取消了。雷同 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

Responding to Remote-Control Events

[plain]  view plain copy
  1. 1.- (void)remoteControlReceivedWithEvent:(UIEvent *)event  
接收到一个远程控制事件。比如耳机控制。
允许传递远程控制事件,必须调用UIApplication的beginReceivingRemoteControlEvents方法;关闭远程控制,调用endReceivingRemoteControlEvents。

Getting the Undo Manager

[plain]  view plain copy
  1. 1.@property(readonly) NSUndoManager *undoManager  
返回在响应链中最近的共享undo manager。
默认的,每个应用程序的window都有一个undo manager:a shared object for managing undo and redo operations.然而,在响应链中任何对象的类都有它们自己的undo manager,

Validating Commands

[plain]  view plain copy
  1. 1.- (BOOL)canPerformAction:(SEL)action withSender:(id)sender  

YES if the the command identified by action should be enabled or NO if it should be disabled. Returning YES means that your class can handle the command in the current context.

原文出处http://blog.csdn.net/lixuwen521/article/details/9664975


-》UIKeyCommand  ios7新增

用于捕获蓝牙键盘的按键事件,例:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (NSArray *)keyCommands   
  2. {  
  3.     return @[[UIKeyCommand keyCommandWithInput:@"f"  
  4.                                  modifierFlags:UIKeyModifierCommand    
  5.                                         action:@selector(searchKeyPressed:)]];  
  6. }  
  7.   
  8. - (void)searchKeyPressed:(UIKeyCommand *)keyCommand   
  9. {  
  10.     // Respond to the event  
  11. }  

9. UIScreen 浅析

-》应用需要将屏幕亮度调整到比中等亮度偏暗的级别
@property(nonatomic) BOOL wantsSoftwareDimming NS_AVAILABLE_IOS(5_0);

- (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel NS_AVAILABLE_IOS(4_0);


-》CADisplayLink 用CADisplayLink可以实现不停重绘。功能类似NSTimer
自從 iOS SDK 3.1 起就增加了 CADisplayLink Class,這個 Class 的功能類似於 Timer。由於能支援每秒高達 60 fps 的畫面同步功能,所以更適合用在製作遊戲動畫上面,相較之下 Timer 較常使用在背景處理層面。

CADisplayLink* gameTimer;
gameTimer = [CADisplayLink displayLinkWithTarget:self
                                            selector:@selector(updateDisplay:)];
[gameTimer addToRunLoop:[NSRunLoop currentRunLoop]
                    forMode:NSDefaultRunLoopMode];

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0); 截屏



10.UIScrollView

-》iOS7新增属性,键盘随滚动消失。

typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {

    UIScrollViewKeyboardDismissModeNone,

    UIScrollViewKeyboardDismissModeOnDrag,      // dismisses the keyboard when a drag begins

    UIScrollViewKeyboardDismissModeInteractive, // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss

} NS_ENUM_AVAILABLE_IOS(7_0);


-》常见属性和方法介绍
子类可以重载
touchesShouldBegin:withEvent:inContentView: 决定自己是否接收 touch 事件

pagingEnabled:当值是 YES 会自动滚动到 subview 的边界,默认是NO

touchesShouldCancelInContentView: 开始发送 tracking messages 消息给 subview 的时候调用这个方法,决定是否发送 tracking messages 消息到subview。假如返回 NO,发送。YES 则不发送。
假如 canCancelContentTouches属性是NO,则不调用这个方法来影响如何处理滚动手势。

    scroll view 还处理缩放和平移手势,要实现缩放和平移,必须实现委托 viewForZoomingInScrollView:、scrollViewDidEndZooming:withView:atScale:
两个方法。另外 maximumZoomScale和minimumZoomScale 两个属性要不一样。

几个属性介绍

tracking
当 touch 后还没有拖动的时候值是YES,否则NO

zoomBouncing
当内容放大到最大或者最小的时候值是 YES,否则 NO

zooming
当正在缩放的时候值是 YES,否则 NO

delaysContentTouches

是个布尔值,当值是 YES 的时候,用户触碰开始,scroll view要延迟一会,看看是否用户有意图滚动。假如滚动了,那么捕捉 touch-down 事件,否则就不捕捉。假如值是NO,当用户触碰, scroll view 会立即触发 touchesShouldBegin:withEvent:inContentView:,默认是 YES

canCancelContentTouches
当值是 YES 的时候,用户触碰后,然后在一定时间内没有移动,scrollView 发送 tracking events,然后用户移动手指足够长度触发滚动事件,这个时候,scrollView 发送了 touchesCancelled:withEvent: 到 subview,然后 scroView 开始滚动。假如值是 NO,scrollView 发送 tracking events 后,就算用户移动手指,scrollView 也不会滚动。

contentSize
里面内容的大小,也就是可以滚动的大小,默认是0,没有滚动效果。

bounces
默认是 yes,就是滚动超过边界会反弹有反弹回来的效果。假如是 NO,那么滚动到达边界会立刻停止。

bouncesZoom
和 bounces 类似,区别在于:这个效果反映在缩放上面,假如缩放超过最大缩放,那么会反弹效果;假如是 NO,则到达最大或者最小的时候立即停止。

directionalLockEnabled
默认是 NO,可以在垂直和水平方向同时运动。当值是 YES 时,假如一开始是垂直或者是水平运动,那么接下来会锁定另外一个方向的滚动。 假如一开始是对角方向滚动,则不会禁止某个方向

indicatorStyle
滚动条的样式,基本只是设置颜色。总共3个颜色:默认、黑、白

scrollIndicatorInsets
设置滚动条的位置

UIscrollerView详解 请参考 http://blog.csdn.net/ch_soft/article/details/6947695
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值