티스토리 뷰
View Hierarchy - Manipulation
• Add/remove views in IB or using UIView methods
- (void)addSubview:(UIView *)view;
- (void)removeFromSuperview;
• Manipulate the view hierarchy manually:
- (void)insertSubview:(UIView *)view atIndex:(int)index;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)view;
- (void)exchangeSubviewAtIndex:(int)index
withSubviewAtIndex:(int)otherIndex;
Frame 과 Bounds 차이
위에서 View B는 Frame 은 140,65,320,320 이고 Bounds 는 0,0,200,250
회전이 되었어도 Frame 은 smallest rectangle 로 계산 되어짐
Manual Creation
• Views are initialized using -initWithFrame:
CGRect frame = CGRectMake(0, 0, 200, 150);
UIView *myView = [[UIView alloc] initWithFrame:frame];
• Example:
CGRect frame = CGRectMake(20, 45, 140, 21);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[window addSubview:label];
[label setText:@”Number of sides:”];
[label release];
// label now retained by window
Defining Custom Views
• Subclass UIView
• For custom drawing, you override:
- (void)drawRect:(CGRect)rect;
• For event handling, you override:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
☆ drawRect 는 직접 호출하지 않는다.
CGPath
• Two parallel sets of functions for using paths
■ CGContext “convenience” throwaway functions ■ CGPath functions for creating reusable paths
CGContext | CGPath |
CGContextMoveToPoint | CGPathMoveToPoint |
CGContextAddLineToPoint | CGPathAddLineToPoint |
CGContextAddArcToPoint | CGPathAddArcToPoint |
CGContextClosePath | CGPathCloseSubPath |
... | ... |
Simple Path Example(삼각형 그리기)
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] set];
UIRectFill ([self bounds]);
CGContextBeginPath (context);
CGContextMoveToPoint (context, 75, 10);
CGContextAddLineToPoint (context, 10, 150);
CGContextAddLineToPoint (context, 160, 150);
CGContextClosePath (context);
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextDrawPath (context, kCGPathFillStroke);
}
UIImage
• UIKit class representing an image
• Creating UIImages:
■ Fetching image in application bundle
■ Use +[UIImage imageNamed:(NSString *)name]
■ Include file extension in file name, e.g. @”myImage.jpg”
■ Read from file on disk
■ Use -[UIImage initWithContentsOfFile:(NSString *)path]
■ From data in memory
■ Use -[UIImage initWithData:(NSData *)data]
UIImageView
• UIView that draws UIImages
• Properties include:
■ image
■ animatedImages
■ animatedDuration
■ animatedRepeatCount
• contentMode property to align and scale image wrt bounds
View Animation Example
- (void)showAdvancedOptions {
// assume polygonView and optionsView
[UIView beginAnimations:@”advancedAnimations” context:nil]; [UIView setAnimationDuration:0.3];
// make optionsView visible (alpha is currently 0.0)
optionsView.alpha = 1.0;
// move the polygonView down
CGRect polygonFrame = polygonView.frame;
polygonFrame.origin.y += 200;
polygonView.frame = polygonFrame;
[UIView commitAnimations];
}
Knowing When Animations Finish
• UIView animations allow for a delegate
[UIView setAnimationDelegate:myController];
• myController will have callbacks invoked before and after
- (void)animationWillStart:(NSString *)animationID
context:(void *)context;
- (void)animationDidStop:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context;
• Can provide custom selectors if desired, for example
[UIView setAnimationWillStartSelector:
@selector(animationWillStart)];
[UIView setAnimationDidStopSelector:
@selector(animationDidStop)];
Saving State Across App Launches
(어플 자체에 설정값을 두는 건데 전역으로 값이 필요할때 쓸만할듯)
• NSUserDefaults to read and write prefs & state
• Singleton object:
+ (NSUserDefaults *)standardUserDefaults;
• Methods for storing & fetching common types:
- (int)integerForKey:(NSString *)key;
- (void)setInteger:(int)value forKey:(NSString *)key;
- (id)objectForKey:(NSString *)key;
- (void)setObject:(id)value forKey:(NSString *)key;
• Find an appropriate time to store and restore your state
출저 -
ITunes U : iPhone Application Development (Winter 2010) - Tracks
'나름 프로그래밍? > Objective-C' 카테고리의 다른 글
UIWebView (0) | 2010.04.21 |
---|---|
Objective-C 에서의 Singleton (2) | 2010.04.17 |
4. Building an Application; Model, View, Controller; Nib Files; Controls and Target-Action (January 14, 2010) (0) | 2010.04.09 |
3. Custom Classes, Object Lifecycle, Autorelease, Objective-C Properties (January 12, 2010) (0) | 2010.04.08 |
XCode 에서 생긴 My Error Solution (1) | 2010.03.16 |