将property声明到类的实现文件中
#import "BKHypnosisView.h"
//在类的实现文件中声明property
@interface BKHypnosisView()
@property (nonatomic, strong) UIColor *circleColor;
@end
@implementation BKHypnosisView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
self.circleColor = [UIColor lightGrayColor];
}
return self;
}
// 执行自定义的绘制动作
- (void)drawRect:(CGRect)rect
{
// .......
// 设置pen的颜色
//[[UIColor purpleColor] setStroke];
[self.circleColor setStroke];
// .......
}
@end
Class Extension
上面代码中的以下代码,就是Class Extension
@interface BKHypnosisView()
// 声明私有的属性或方法
@property (nonatomic, strong) UIColor *circleColor;
@end
头文件和实现文件中property的区别?
头文件中的就像JAVA中的public,是公有的,其他类可以访问。
实现文件中的就像private,是私有的,只有当前类可以访问。
Run Loop
When an iOS application is launched, it starts a run loop. The run loop’s job is to listen for events, such as a touch. When an event occurs, the run loop then finds the appropriate handler methods for the event. Those handler methods call other methods, which call more methods, and so on. Once all of the methods have completed, control returns to the run loop.
When the run loop regains control, it checks a list of “dirty views” – views that need to be re-rendered based on what happened in the most recent round of event handling. The run loop then sends the drawRect:message to the views in this list before all of the views in the hierarchy are composited together again.
当视图发生变化需要重绘时,需要调用setNeedsDisplay方法
- (void)setCircleColor:(UIColor *)circleColor{
_circleColor = circleColor;
// 当一个视图需要重绘时,调用setNeedsDisplay方法
[self setNeedsDisplay];
}
UIScrollView
Scroll views are typically used for views that are larger than the screen. A scroll view draws a rectangular portion of its subview, and moving your finger, or panning, on the scroll view changes the position of that rectangle on the subview. Thus, you can think of the scroll view as a viewing port that you can move around (如下图). The size of the scroll view is the size of this viewing port. The size of the area that it can be used to view is the UIScrollView’s contentSize, which is typically the size of the UIScrollView’s subview.
#import "BKAppDelegate.h"
#import "BKHypnosisView.h"
@implementation BKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect screenRect = self.window.bounds;
CGRect bigRect = screenRect;
bigRect.size.width *= 2;
// bigRect.size.height *= 2;
// 创建一个屏幕大小的scroll view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
scrollView.pagingEnabled = YES; // 一页一页的移动,不会停留在中间页
[self.window addSubview:scrollView];
// 创建一个是屏幕两倍大小的视图,并添加到scroll view中
BKHypnosisView *myView = [[BKHypnosisView alloc] initWithFrame:screenRect];
[scrollView addSubview:myView];
screenRect.origin.x += screenRect.size.width;
BKHypnosisView *myView2 = [[BKHypnosisView alloc] initWithFrame:screenRect];
[scrollView addSubview:myView2];
// 设置contentSize
scrollView.contentSize = bigRect.size;
// // 将BKHypnosisView设置为全屏,即window的bounds
// CGRect firstFrame = self.window.bounds;
// BKHypnosisView *firstView = [[BKHypnosisView alloc] initWithFrame:firstFrame];
// [self.window addSubview:firstView];
// CGRect secondFrame = CGRectMake(20, 30, 50, 50);
// BKHypnosisView *secondView = [[BKHypnosisView alloc] initWithFrame:secondFrame];
// secondView.backgroundColor = [UIColor blueColor];
//// [self.window addSubview:secondView];
// [firstView addSubview:secondView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
本文是对《iOS Programming The Big Nerd Ranch Guide 4th Edition》第五章的总结。