06 subclassing uiview and uiscrollview

Download 06 Subclassing UIView and UIScrollView

Post on 28-Jun-2015

1.129 views

Category:

Technology

3 download

Embed Size (px)

DESCRIPTION

子类化 UIView 和 UIScrollView

TRANSCRIPT

  • 1. UIView UIScrollViewprincetoad@gmail.comwww.tfan.org

2. view UIButtonUILabel view view UIView view UIWindow view view view hierarchyviewhierarchy view 3. UIView 4. Hypnosister iOS Hypnosister Hypnosister Use Automatic Reference Counting 5. 6. UIButton UILabel view UILabel text UIButton titleview 7. UIWindow UIWindow UIView UIWindow HypnosisterAppDelegate.m application:didFinishLaunchingWithOptions: UIWindow makeKeyAndVisible- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];return YES;} 8. subview window view view window subview window subview view subviews window subview window view subview window view hierarchy root 9. redrawn window window subview subviews subviews 10. view hierarchy 11. view view view hierarchy UIButtonMKMapView UITextField Apple 12. 13. UIView HypnosisView, UIView HypnosisterAppDelegate.m HypnosisView.h application:didFinishLaunchingWithOptions: HypnosisView window subview view hierarchy 14. HypnosisView UIWindow- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];CGRect viewFrame = CGRectMake(160, 240, 100, 150);HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];[view setBackgroundColor:[UIColor redColor]];[[self window] addSubview:view];// Override point for customization after application launch.self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];return YES;} 15. subview superview HypnosisView UIWindow HypnosisView UIWindow subview view view subview HypnosisView superview UIWindow XIB 16. initWithFrame: view alloc initializer message UIView designated initializer HypnosisViewinitWithFrame: initWithFrame: CGRect CGRect view 17. view frame frame frame superview frame CGRect origin size orgin CGPoint float x y size CGSize float width height structure objective-c 18. CGRect view 19. HypnosisView Window subview // HypnosisView CGRect anotherFrame = CGRectMake(20, 30, 50, 50); HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:anotherFrame]; [anotherView setBackgroundColor:[UIColor blueColor]]; [[self window] addSubview:anotherView]; 20. HypnosisViews window subview view hierarchy 21. HypnosisView HypnosisView subview view frame superview window[[self window] addSubview:anotherView];[view addSubview:anotherView]; 22. HypnosisView HypnosisView subview view hierarchy 23. drawRect: 24. UIView view hierarchy backgroundColor iOS view UIView drawRect: drawRect: UIView 25. Core Graphic framework drawRect: UIView Core Graphics framework application target 26. drawing context drawRect: drawing context drawRect: view 27. CGContextRef CGContextRef CGContext * - CGContext Ref C Objective-C ctx - (void)drawRect:(CGRect)rect{CGContextRef ctx = UIGraphicsGetCurrentContext();} 28. bounds view view frame frame view view superview UIView bounds CGRect superview CGContextRef bounds 29. bounds - (void)drawRect:(CGRect)rect{CGContextRef ctx = UIGraphicsGetCurrentContext();CGRect bounds = [self bounds];// bounds CGPoint center;center.x = bounds.origin.x + bounds.size.width / 2.0;center.y = bounds.origin.y + bounds.size.height / 2.0;// view float maxRadius = hypot(bounds.size.width, bounds.size.height) / 4.0;// 10pt CGContextSetLineWidth(ctx, 10);// (red/green/blue = 0.6, alpha = 1.0)CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0);// - CGContextAddArc(ctx, center.x, center.y, maxRadius, 0.0, M_PI * 2.0, YES);// CGContextStrokePath(ctx);} 30. bounds 31. backgroundColor HyposisterAppDelegate.m view HypnosisView.m initWithFrame: HyposisView clear HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame]; [view setBackgroundColor:[UIColor redColor]]; [[self window] addSubview:view]; CGRect anotherFrame = CGRectMake(20, 30, 50, 50); HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:anotherFrame]; [anotherView setBackgroundColor:[UIColor blueColor]]; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setBackgroundColor:[UIColor clearColor]]; } return self; } 32. clearColor 33. Core Graphics 34. Core Graphics CG Core Graphics framework, 2D C API Core Graphics framework CGContextRef: Core Graphics Core Graphics CGContextSetLineWidth CGContextSetRGBStrokeColor CGContextAddArc path Arc path 35. CGContextStrokePath CGContextFillPaht CGContextClip 36. // view // float maxRadius = hypot(bounds.size.width, bounds.size.height) / 4.0; float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0; // 10pt CGContextSetLineWidth(ctx, 10); // (red/green/blue = 0.6, alpha = 1.0) CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0); for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES); CGContextStrokePath(ctx); } 37. HypnosisView HypnosisView 38. didFinishLaunchingWithOptions:- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; // HypnosisViewHypnosisView *view = [[HypnosisViewalloc] initWithFrame:[[self window]bounds]];[[self window] addSubview:view];// Override point for customizationafter application launch.self.window.backgroundColor = [UIColorwhiteColor];[self.window makeKeyAndVisible];return YES;} 39. UIKit Foundation UIKit CGContextRef 40. UIColor UIColor UIColor CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0) [[UIColor colorWithRed: 0.6 green:0.6 blue: 0.6 alpha:1] setStroke]; UIColor [[UIColor lightGrayColor] setStroke];CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0);[[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1] setStroke]; 41. NSString NSString CGContextRef drawInRect:withFont: NSString HyposisView.m drawRect: NSString *text = @"."; UIFont *font = [UIFont boldSystemFontOfSize:28]; CGRect textRect; textRect.size = [text sizeWithFont:font]; textRect.origin.x = center.x - textRect.size.width / 2.0; textRect.origin.y = center.y - textRect.size.height / 2.0; [[UIColor blackColor] setFill]; [text drawInRect:textRect withFont:font]; 42. 43. // [[UIColor blackColor] setFill]; // 4points3points CGSize offset = CGSizeMake(4, 3); // CGColorRef color = [[UIColor darkGrayColor] CGColor]; // , (blur,,2.0) CGContextSetShadowWithColor(ctx, offset, 2.0, color); // [text drawInRect:textRect withFont:font]; 44. 45. UIImage drawInRect: image 46. 47. UIView setNeedsDisplay setNeedsDisplay UILabel setText: run loop 48. Run Loop 49. circleColor HypnosisView.h circleColor HypnosisView.m synthesize initWithFrame: circleColor drawRect: circleColor light gray 50. circleColor @interface HypnosisView : UIView@property (nonatomic, strong) UIColor *circleColor;@end@implementation HypnosisView@synthesize circleColor;- (id)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self) {[self setBackgroundColor:[UIColor clearColor]];[self setCircleColor:[UIColor lightGrayColor]];}return self;} CGContextSetLineWidth(ctx, 10); [[self circleColor] setStroke]; for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES); CGContextStrokePath(ctx); } 51. (Motion Events) 52. UIView UIResponder window first responder HypnosisView Hypnosister window first responder HypnosisView circleColor 53. becomeFirstResponder HypnosisterAppDelegate.m HypnosisView first responder becomeFirstResponder window first responderBOOL success = [view becomeFirstResponder];if (success) {NSLog(@"HypnosisView first responder");} else {NSLog(@" first responder");} 54. canBecomeFirstResponder UIResponder becomeFirstResponder NO first responder UIButton responder first responder HypnosisView.m UIResponder canBecomeFirstResponder YES- (BOOL)canBecomeFirstResponder{ return YES; } 55. motion event methods UIResponder UIResponder shakes UIResponder motion event methods(,- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event 56. motionBegan:withEvent: HypnosisView motionBegan:withEvent: HypnosisView.m circleColor- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event{NSLog(@"");[self setCircleColor:[UIColor redColor]];} 57. setNeedsDisplay HypnosisView circleColor circleColor UIColor HypnosisView HypnosisView circleColor setNeedsDisplay HypnosisView.m setCircleColor: circleColor - (void)setCircleColor:(UIColor *)clr{circleColor = clr;[self setNeedsDisplay];} 58. UIEventSubtype UIEventSubtype shake HypnosisView.m if (motion == UIEventSubtypeMotionShake)typedef NS_ENUM(NSInteger, UIEventSubtype) {UIEventSubtypeNone= 0,UIEventSubtypeMotionShake = 1,UIEventSubtypeRemoteControlPlay = 100,UIEventSubtypeRemoteControlPause= 101,UIEventSubtypeRemoteControlStop = 102,UIEventSubtypeRemoteControlTogglePlayPause= 103,UIEventSubtypeRemoteControlNextTrack= 104,UIEventSubtypeRemoteControlPreviousTrack= 105,UIEventSubtypeRemoteControlBeginSeekingBackward = 106,UIEventSubtypeRemoteControlEndSeekingBackward = 107,UIEventSubtypeRemoteControlBeginSeekingForward= 108,UIEventSubtypeRemoteControlEndSeekingForward= 109,}; 59. UIScrollView 60

Recommended

View more >