你的位置:首页 > 操作系统

[操作系统]内外分离接口依赖及UIScrollView知识点


1:Class Extension 还能巧妙的解决一个接口暴露问题

有些属性或者方法对外可以提供,有些只针对内部的类进行调用;

// Sark.framework/Sark.h@interface Sark : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *creditCardPassword; // secret!@end// Sark.framework/PrivateSarkWife.h@interface PrivateSarkWife : NSObject- (void)robAllMoneyFromCreditCardOfSark:(Sark *)sark; // needs password!@end

假设 Sark.h 是 Sark.framework 唯一暴露的 Header,而 framework 中的一个私有类需要获取这个公共类的某个属性(或方法)该怎么办?PrivateSarkWife为Sark.framework内部一个类;上面的 creditCardPassword 属性需要一个对外不可见而对内可见的地方声明,这时候可以利用 Class Extension:

// Sark.h@interface Sark : NSObject@property (nonatomic, copy) NSString *name;@end// Sark+Internal.h <--- new@interface Sark ()@property (nonatomic, copy) NSString *creditCardPassword;@end// Sark.m#import "Sark.h"#import "Sark+Internal.h" // <--- new

将对公业务和对私业务用 Class Extension 的形式拆到两个 Header 中,在Sark.m里把两个头文件都进行引用,CreditCardPassword被定义在Sark+Internal.h的头文件里面,这样私有类对私有属性的依赖就被成功隔离开了:

// PrivateSarkWife.m#import "PrivateSarkWife.h"#import "Sark+Internal.h" // <--- 私有依赖@implementation PrivateSarkWife- (void)robAllMoneyFromCreditCardOfSark:(Sark *)sark {  NSString *password = sark.creditCardPassword; // oh yeah!}@end

 

2:UIScrollView知识点

- (void)viewDidLoad{  [super viewDidLoad];     scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];  scrollView.backgroundColor = [UIColor redColor];  // 是否支持滑动最顶端//  scrollView.scrollsToTop = NO;  scrollView.delegate = self;  // 设置内容大小  scrollView.contentSize = CGSizeMake(320, 460*10);  // 是否反弹//  scrollView.bounces = NO;  // 是否分页//  scrollView.pagingEnabled = YES;  // 是否滚动//  scrollView.scrollEnabled = NO;//  scrollView.showsHorizontalScrollIndicator = NO;  // 设置indicator风格//  scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;  // 设置内容的边缘和Indicators边缘//  scrollView.contentInset = UIEdgeInsetsMake(0, 50, 50, 0);//  scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 50, 0, 0);  // 提示用户,Indicators flash  [scrollView flashScrollIndicators];  // 是否同时运动,lock  scrollView.directionalLockEnabled = YES;  [self.view addSubview:scrollView];     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)];  label.backgroundColor = [UIColor yellowColor];  label.text = @"学习scrolleview";  [scrollView addSubview:label];  [label release];} #pragma mark - /*// 返回一个放大或者缩小的视图- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{   }// 开始放大或者缩小- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{   } // 缩放结束时- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{   } // 视图已经放大或缩小- (void)scrollViewDidZoom:(UIScrollView *)scrollView{NSLog(@"scrollViewDidScrollToTop");} */ // 是否支持滑动至顶部- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{  return YES;} // 滑动到顶部时调用该方法- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{  NSLog(@"scrollViewDidScrollToTop");} // scrollView 已经滑动- (void)scrollViewDidScroll:(UIScrollView *)scrollView{  NSLog(@"scrollViewDidScroll");} // scrollView 开始拖动- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{  NSLog(@"scrollViewWillBeginDragging");} // scrollView 结束拖动- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{  NSLog(@"scrollViewDidEndDragging");} // scrollView 开始减速(以下两个方法注意与以上两个方法加以区别)- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{  NSLog(@"scrollViewWillBeginDecelerating");} // scrollview 减速停止- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{  NSLog(@"scrollViewDidEndDecelerating"); }