转自:http://langhua9527.iteye.com/blog/1377741
如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类
- performSelector:
- performSelector:withObject:
- performSelector:withObject:withObject:
实际调用
- [self performSelector:@selector(displayViews) withObject:nil afterDelay:1.0f];
有三个方法分别是
-
- [self.view superview]
-
- [self.view subviews]
-
- self.view.window
循环一个视图下面所有视图的方法
- NSArray *allSubviews(UIView *aView)
- {
- NSArray *results = [aView subviews];
- for (UIView *eachView in [aView subviews])
- {
- NSArray *riz = allSubviews(eachView);
- if (riz) {
- results = [results arrayByAddingObjectsFromArray:riz];
- }
- }
- return results;
- }
循环返回一个APPLICATION里面所有的VIEW
-
- NSArray *allApplicationViews()
- {
- NSArray *results = [[UIApplication sharedApplication] windows];
- for (UIWindow *window in [[UIApplication sharedApplication] windows])
- {
- NSArray *riz = allSubviews(window);
- if (riz) results = [results arrayByAddingObjectsFromArray: riz];
- }
- return results;
- }
找出所有的父视图
-
- NSArray *pathToView(UIView *aView)
- {
- NSMutableArray *array = [NSMutableArray arrayWithObject:aView];
- UIView *view = aView;
- UIWindow *window = aView.window;
- while (view != window)
- {
- view = [view superview];
- [array insertObject:view atIndex:0];
- }
- return array;
- }
UIView提供了大量管理视图的方法
-
- addSubview:
-
- bringSubviewToFront:
-
- sendSubviewToBack:
-
- removeFromSuperview
-
- insertSubview:atIndex:
-
- insertSubview:aboveSubview:
-
- insertSubview:belowSubview:
-
- exchangeSubviewAtIndex:withSubviewAtIndex:
视图回调
-
- (void)didAddSubview:(UIView *)subview
-
- (void)didMoveToSuperview
-
- (void)didMoveToWindow
-
- (void)willRemoveSubview:(UIView *)subview
-
- (void)didMoveToSuperview:(UIView *)subview
-
- (void)didMoveToWindow
给UIView设置标记和检索视图
- myview.tag = 1001;
- [self.view viewWithTag:1001];
- (UILable *)[self.view.window viewWithTag:1001];
视图的几何特征
-
- struct CGPoint {
- CGFloat x;
- CGFloat y;
- };
- typedef struct CGPoint CGPoint;
-
-
-
- struct CGSize {
- CGFloat width;
- CGFloat height;
- };
- typedef struct CGSize CGSize;
-
- struct CGRect {
- CGPoint origin;
- CGSize size;
- };
- typedef struct CGRect CGRect;
-
-
-
- CGRect rect = CGRectMake(0,0,320,480);
- UIView *view = [[UIView allow]initWithFrame:rect];
-
-
- CGPoint CGPointFromString (
- NSString *string
- );
-
-
- CGRect CGRectFromString (
- NSString *string
- );
-
-
- CGSize CGSizeFromString (
- NSString *string
- );
-
-
- NSString * NSStringFromCGPoint (
- CGPoint point
- );
-
-
- NSString * NSStringFromCGRect (
- CGRect rect
- );
-
-
- NSString * NSStringFromCGSize (
- CGSize size
- );
-
-
- CGRect CGRectInset (
- CGRect rect,
- CGFloat dx,
- CGFloat dy
- );
-
-
- bool CGRectIntersectsRect (
- CGRect rect1,
- CGRect rect2
- );
-
-
- const CGPoint CGPointZero;
- const CGRect CGRectZero;
- const CGSize CGSizeZero;
-
-
- CGPoint CGPointMake (
- CGFloat x,
- CGFloat y
- );
-
- CGRect CGRectMake (
- CGFloat x,
- CGFloat y,
- CGFloat width,
- CGFloat height
- );
-
- CGSize CGSizeMake (
- CGFloat width,
- CGFloat height
- );
仿射变换
- CGAffineTransform form = CGAffineTransformMakeRotation(PI);
- myview.transform = form;
如想复原
- myview.transform = CGAffineTransformIdentity;
直接设置视图的中心
- myview.center = CGPointMake(100,200);
中心
- CGRectGetMinX
- CGRectGetMinY
-
- CGRectGetMidX
-
- CGRectGetMidY
- CGRectGetMaxX
- CGRectGetMaxY
定时器
- NSTime *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(move:) userInfo:nil repeats:YES];
定义视图边界
- typedef struct UIEdgeInsets {
- CGFloat top, left, bottom, right;
- } UIEdgeInsets;
-
- UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5);
- CGRect innerRect = UIEdgeInsetsInsetRect([aView bounds], insets);
- CGRect subRect = CGRectInset(innerRect, self.frame.size.width / 2.0f, self.frame.size.height / 2.0f);
仿射变换补充
//创建CGAffineTransform
-
- CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
-
- CGAffineTransform transform = CGAffineTransformMakeScale(0.5f,0.5f);
-
- CGAffineTransform transform = CGAffineTransformMakeTranslation(50,60);
-
-
-
- CGAffineTransform scaled = CGAffineTransformScale(transform, degree, degree);
-
- CGAffineTransform transform = CGAffineTransformTranslate(
- CGAffineTransform t,
- CGFloat tx,
- CGFloat ty
- );
-
-
- CGAffineTransform transform = CGAffineTransformRotate (
- CGAffineTransform t,
- CGFloat angle
- );
-
- [self.view setTransform:scaled];
建立UIView动画块
//首先建立CGContextRef
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- [UIView beginAnimations:nil context:context];
-
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
-
- [UIView setAnimationDuration:1.0];
-
- [[self.view viewWithTag:999] setAlpha:1.0f];
-
- [UIView commitAnimations];
-
- [UIView setAnimationDelegate:self];
-
- [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
视图翻转
- UIView *whiteBackdrop = [self.view viewWithTag:100];
-
- if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]){
- [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:whiteBackdrop cache:YES];
- }else{
- [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:whiteBackdrop cache:YES];
- }
- NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]];
- NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]];
-
- [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];
-
-
- typedef enum {
-
- UIViewAnimationTransitionNone,
- UIViewAnimationTransitionFlipFromLeft,
- UIViewAnimationTransitionFlipFromRight,
- UIViewAnimationTransitionCurlUp,
- UIViewAnimationTransitionCurlDown,
- } UIViewAnimationTransition;
使用QuartzCore做动画
-
- CATransition *animation = [CATransition animation];
-
- animation.delegate = self;
-
- animation.duration = 4.0f;
-
- animation.timingFunction = UIViewAnimationCurveEaseInOut;
-
- switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) {
- case 0:
- animation.type = kCATransitionFade;
- break;
- case 1:
- animation.type = kCATransitionMoveIn;
- break;
- case 2:
- animation.type = kCATransitionPush;
- break;
- case 3:
- animation.type = kCATransitionReveal;
- default:
- break;
- }
-
- if (isLeft)
- animation.subtype = kCATransitionFromRight;
- else
- animation.subtype = kCATransitionFromLeft;
-
-
- UIView *whitebg = [self.view viewWithTag:10];
- NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:99]];
- NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:100]];
- [whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white];
- [[whitebg layer] addAnimation:animation forKey:@"animation"];
animation.type还可以用以下的赋值
- switch (theButton.tag) {
- case 0:
- animation.type = @"cube";
- break;
- case 1:
- animation.type = @"suckEffect";
- break;
- case 2:
- animation.type = @"oglFlip";
- break;
- case 3:
- animation.type = @"rippleEffect";
- break;
- case 4:
- animation.type = @"pageCurl";
- break;
- case 5:
- animation.type = @"pageUnCurl";
- break;
- case 6:
- animation.type = @"cameraIrisHollowOpen ";
- break;
- case 7:
- animation.type = @"cameraIrisHollowClose ";
- break;
- default:
- break;
- }
上面这个是转自这里的http://2015.iteye.com/blog/1122130
休眠一下
- [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];
一个简单的通过图片做的动画
-
- NSMutableArray *bflies = [NSMutableArray array];
- for (int i = 1; i <= 17; i++){
- [bflies addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"bf_%d", i] ofType:@"png"]]];
- }
- UIImageView *butterflyView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0f, 300.0f, 60.0f, 60.0f)];
- butterflyView.tag = 300;
-
- butterflyView.animationImages = bflies;
-
- butterflyView.animationDuration = 0.75f;
- [self.view addSubview:butterflyView];
-
- [butterflyView startAnimating];
- [butterflyView release];
UIView总结
原文:http://www.cnblogs.com/wangpei/p/3539041.html