[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showBeautifulChanges:) userInfo:bgView repeats:YES];
这里的bgView是由定时器传给某方法的信息,也就是说在某函数中用到showbeatifulChanges:这个方法的时候也可以使用bgView这个视图的信息。函数传进去参数NStimer的同时也可以使用bgView。
可以用subViews取某一个视图的所有子视图,之后放到一个数组中就都可以使用了。
UIImage * currentImage=[button backgroundImageForState:UIControlStateNormal];
这是取button的背景图片
改变背景图片可以使用currentImage的名字是某和某图片相等。如:
if ([currentImage isEqual :[ UIImage imageNamed : @"red.png" ]]){
[button setBackgroundImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal];
}
else
{
[button setBackgroundImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateNormal];
}
greenView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
可以使用autoresizingMask使子视图跟随父视图一起变化,并且可以指定一起变化的内容
CGrectInset(aFrame,x,y)作用是得到一个新的frame,此frame的值为(aFrame.origin.x-x,aFrame.origon.y - y,aFrame.size+2*x,aFrame.size+2*y)
UIView动画包括frame/bounds/center/transform/alpha/backgroundColor/contentStretch等
1.开始和结束UIVIew动画使用beginAnimations和commitAnimations
2.视图动画可以通知可选委托其状态的改变,即动画已经开始或者结束。当需要捕获动画结束时间并开始序列中下一段动画时,这个功能非常有用。比如可以让几个动画连续发生或者同时发生。
设置委托使用:[UIView setAnimationDelegate:self];
若要设置动画结束回调,就要提供要发给委托的选择器。
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
这样在当前的实现文件中实现下面的方法:-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
finished这个参数作用很大,正常结束finished的值为1,否则为0。
(void)beginAnimations:(NSString *)animationID context:(void *)context;animationID标记动画ID,context为id类型,可传入任意类型对象。这个函数很有用,比如需要顺序执行几个动画,就可以使用到context的功能了
- (void)viewDidLoad
{
[super viewDidLoad];
imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)];
imageView.image=[UIImage imageNamed:@"气球.png"];
[self.view addSubview:imageView];
[imageView release];
[UIView beginAnimations:nil context:@"move"];
[UIView setAnimationDuration:1];
[UIView setAnimationDidStopSelector:@selector(animationDidStop: finished:context:)];
[UIView setAnimationDelegate:self];
imageView.frame=CGRectMake(100, 300, 100, 100);
[UIView commitAnimations];
// Do any additional setup after loading the view.
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
NSString * str=(NSString*)context;
if ([str isEqualToString:@"move"]) {
[UIView beginAnimations:nil context:@"flip"];
[UIView setAnimationDuration:1];
[UIView setAnimationDidStopSelector:@selector(animationDidStop: finished:context:)];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:imageView cache:YES];
[UIView commitAnimations];
}
if ([str isEqualToString:@"flip"]) {
CGAffineTransform transform=CGAffineTransformScale(imageView.transform, 1.5, 1.5);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[imageView setTransform:transform];
[UIView commitAnimations];
}
}
[UIView setAnimationBeginsFromCurrentState:YES]这个方法告诉系统,如果有两个方法发生在一个view上,而且前一个动画还没有结束后一个动画就开始的情况下,这样会将动画进行的当前状态座位下一个状态的开始,避免了动画跳跃性的不连贯感觉。
(void)setAnimationTransition:(UIViewAnimationTransition)transition forView(UIView *)view cache:(BOOL)cache;调用这个方法就可以实现动画的过度效果,(UIView *)view决定哪个view要进行过渡效果,(BOOL)ceche参数告诉系统是否用cache进行动画(与硬件有关)一般填YES。
UIApplication继承于UIResponder,实现了UIApplicationDelegate代理,每一个应用程序都有一个UIApplication或者子类型的实例。
代理方法:
//应用程序转入激活状态
- (BOOL) application:(UIApplication *) application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//应用程序从激活状态转入非激活状态(响应home键)
- applicationDidEnterBackground:(UIApplication *) application
//应用程序启动
- applicationDidFinishLaunching: (UIApplication *)application
//应用程序终止时
- applicationWillTerminate: (UIApplication *) application
//应用程序转入后台(非激活)状态
(void)applicationWillResignActive:(UIApplication *) application