IOS项目常用代码
2012-12-26 14:26
357 查看
1.增加一个旋转动画
UIImage *loadImage = [UIImageimageNamed:@"detailLoad.png"];
UIImageView *loadImageView = [[[UIImageViewalloc]initWithImage:loadImage ]autorelease];
loadImageView.backgroundColor = [UIColorclearColor];
loadImageView.center =self.view.center;
loadImageView.tag =kLoadImageView;
//imageview旋转动画
CAKeyframeAnimation *theAnimation = [CAKeyframeAnimationanimation];
theAnimation.values = [NSArrayarrayWithObjects:
[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(0,0,0,1)],
[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.13,0,0,1)],
[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(6.26,0,0,1)],
nil];
theAnimation.cumulative =YES;
theAnimation.removedOnCompletion =YES;
theAnimation.repeatCount =HUGE_VALF;
[theAnimation setSpeed:.15];
[loadImageView.layeraddAnimation:theAnimationforKey:@"transform"];
[self.viewaddSubview:loadImageView];
2.图片自动放大缩小
UIImage *infoImage = [UIImageimageNamed:@"detail_info_btn.png"];
UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,
infoImage.size.width, infoImage.size.height)];
[infoButton setTag:kInfoButtonTag];
[infoButtonsetImage:infoImageforState:UIControlStateNormal];
[infoButtonaddTarget:selfaction:@selector(infoButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:infoButton];
[infoButton release];
//变大缩小动画
CAKeyframeAnimation* bob = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];
CATransform3D r[3] = {CATransform3DMakeScale(1.0,1.0,1),
CATransform3DMakeScale(1.2,1.2,1)};
bob.values = [NSArrayarrayWithObjects:[NSValuevalueWithCATransform3D:r[0]],
[NSValuevalueWithCATransform3D:r[1]],[NSValuevalueWithCATransform3D:r[0]],nil];
bob.repeatCount =HUGE_VAL;
bob.duration =1.75;
bob.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
[infoButton.layer addAnimation:bobforKey:nil];
3.截取当前屏幕作为 一张图
//开始截图
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage *img =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
4.让view 以任意方式 push出来
- (void)contentViewAniamted:(NSString *)subtype :(UIView *)currentView{
CATransition *animation = [CATransitionanimation];
animation.duration =.3;
animation.timingFunction =UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards; //必须设定 不然不生效
animation.type = kCATransitionPush;
animation.subtype = subtype;
[currentView.layer addAnimation:animationforKey:@"animation"];
[self.viewaddSubview:currentView];
}
- (void)show
{
LeftView *currentLeft = [[[LeftViewalloc]initWithFrame:CGRectMake(0,0,600,566)
]autorelease];
currentLeft.center =self.view.center;
[selfcontentViewAniamted:kCATransitionFromTop :currentLeft];
// [self makeZSViewControllerInVisible:currentLeft];
[self.view addSubview:currentLeft];
}
5.图片以中心点 放大
//执行动画
[UIView beginAnimations:@""context:@""];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.bigImageView.frame =CGRectMake(self.bigImageView.frame.origin.x -
bigImage.size.width/2 ,self.bigImageView.frame.origin.y -
bigImage.size.height/2, bigImage.size.width,
bigImage.size.height);
[UIViewcommitAnimations];
6.让两个viewController以动画形式翻出
CATransition *transition = [CATransitionanimation];
transition.duration =1;
transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type =kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate =self;
[self.navigationController.view.layeraddAnimation:transitionforKey:nil];
[self.navigationControllerpopViewControllerAnimated:NO];
7.当键盘出现时 控制view的移动
//通过键盘出现或者消失 来控制view上移
//通过键盘出现或者消失 来控制view上移
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
[center addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
-(void)keyboardWillShow:(NSNotification *)note
{
if (self.contentView.frame.origin.y <0)return;
[UIViewanimateWithDuration:.4
animations:^(void) {
self.contentView.center=CGPointMake(self.contentView.center.x,self.contentView.center.y -300);
}];
}
-(void)keyboardWillHide:(NSNotification *)note
{
if (self.contentView.frame.origin.y ==0)return;
[UIViewanimateWithDuration:.4
animations:^(void) {
self.contentView.center=CGPointMake(self.contentView.center.x,self.contentView.center.y +300);
}];
}
8.把字符串 存储到一个文件里
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0]
: nil;
NSString *contentJsonPath = [basePath stringByAppendingPathComponent:@"content_.json"];
NSString *thumsPath = [basePath stringByAppendingPathComponent:@"thumbs.png"];
NSString *imgPath = [basePath stringByAppendingPathComponent:@"img.png"];
//将json文件保存起来
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[contentJson writeToFile:contentJsonPath atomically:YES encoding:NSUTF8StringEncodingerror:NULL];
[thumbs writeToFile:thumsPath atomically:YES];
[img writeToFile:imgPath atomically:YES];
[pool drain];
UIImage *loadImage = [UIImageimageNamed:@"detailLoad.png"];
UIImageView *loadImageView = [[[UIImageViewalloc]initWithImage:loadImage ]autorelease];
loadImageView.backgroundColor = [UIColorclearColor];
loadImageView.center =self.view.center;
loadImageView.tag =kLoadImageView;
//imageview旋转动画
CAKeyframeAnimation *theAnimation = [CAKeyframeAnimationanimation];
theAnimation.values = [NSArrayarrayWithObjects:
[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(0,0,0,1)],
[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.13,0,0,1)],
[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(6.26,0,0,1)],
nil];
theAnimation.cumulative =YES;
theAnimation.removedOnCompletion =YES;
theAnimation.repeatCount =HUGE_VALF;
[theAnimation setSpeed:.15];
[loadImageView.layeraddAnimation:theAnimationforKey:@"transform"];
[self.viewaddSubview:loadImageView];
2.图片自动放大缩小
UIImage *infoImage = [UIImageimageNamed:@"detail_info_btn.png"];
UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,
infoImage.size.width, infoImage.size.height)];
[infoButton setTag:kInfoButtonTag];
[infoButtonsetImage:infoImageforState:UIControlStateNormal];
[infoButtonaddTarget:selfaction:@selector(infoButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:infoButton];
[infoButton release];
//变大缩小动画
CAKeyframeAnimation* bob = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];
CATransform3D r[3] = {CATransform3DMakeScale(1.0,1.0,1),
CATransform3DMakeScale(1.2,1.2,1)};
bob.values = [NSArrayarrayWithObjects:[NSValuevalueWithCATransform3D:r[0]],
[NSValuevalueWithCATransform3D:r[1]],[NSValuevalueWithCATransform3D:r[0]],nil];
bob.repeatCount =HUGE_VAL;
bob.duration =1.75;
bob.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
[infoButton.layer addAnimation:bobforKey:nil];
3.截取当前屏幕作为 一张图
//开始截图
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage *img =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
4.让view 以任意方式 push出来
- (void)contentViewAniamted:(NSString *)subtype :(UIView *)currentView{
CATransition *animation = [CATransitionanimation];
animation.duration =.3;
animation.timingFunction =UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards; //必须设定 不然不生效
animation.type = kCATransitionPush;
animation.subtype = subtype;
[currentView.layer addAnimation:animationforKey:@"animation"];
[self.viewaddSubview:currentView];
}
- (void)show
{
LeftView *currentLeft = [[[LeftViewalloc]initWithFrame:CGRectMake(0,0,600,566)
]autorelease];
currentLeft.center =self.view.center;
[selfcontentViewAniamted:kCATransitionFromTop :currentLeft];
// [self makeZSViewControllerInVisible:currentLeft];
[self.view addSubview:currentLeft];
}
5.图片以中心点 放大
//执行动画
[UIView beginAnimations:@""context:@""];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.bigImageView.frame =CGRectMake(self.bigImageView.frame.origin.x -
bigImage.size.width/2 ,self.bigImageView.frame.origin.y -
bigImage.size.height/2, bigImage.size.width,
bigImage.size.height);
[UIViewcommitAnimations];
6.让两个viewController以动画形式翻出
CATransition *transition = [CATransitionanimation];
transition.duration =1;
transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type =kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate =self;
[self.navigationController.view.layeraddAnimation:transitionforKey:nil];
[self.navigationControllerpopViewControllerAnimated:NO];
7.当键盘出现时 控制view的移动
//通过键盘出现或者消失 来控制view上移
//通过键盘出现或者消失 来控制view上移
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
[center addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
-(void)keyboardWillShow:(NSNotification *)note
{
if (self.contentView.frame.origin.y <0)return;
[UIViewanimateWithDuration:.4
animations:^(void) {
self.contentView.center=CGPointMake(self.contentView.center.x,self.contentView.center.y -300);
}];
}
-(void)keyboardWillHide:(NSNotification *)note
{
if (self.contentView.frame.origin.y ==0)return;
[UIViewanimateWithDuration:.4
animations:^(void) {
self.contentView.center=CGPointMake(self.contentView.center.x,self.contentView.center.y +300);
}];
}
8.把字符串 存储到一个文件里
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0]
: nil;
NSString *contentJsonPath = [basePath stringByAppendingPathComponent:@"content_.json"];
NSString *thumsPath = [basePath stringByAppendingPathComponent:@"thumbs.png"];
NSString *imgPath = [basePath stringByAppendingPathComponent:@"img.png"];
//将json文件保存起来
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[contentJson writeToFile:contentJsonPath atomically:YES encoding:NSUTF8StringEncodingerror:NULL];
[thumbs writeToFile:thumsPath atomically:YES];
[img writeToFile:imgPath atomically:YES];
[pool drain];
相关文章推荐
- iOS项目开发经验:【常用代码4】
- iOS项目开发经验:【常用代码2】
- iOS项目开发经验:【常用代码3】
- iOS项目开发经验:【常用代码5】
- iOS项目开发经验:【常用代码1】
- ios项目常用代码
- iOS项目 我常用的代码
- IOS项目常用代码
- iOS项目 我常用的代码
- iOS项目开发经验:【常用代码7】
- iOS项目开发经验:【常用代码6】
- iOS入门-了解这些常用单词,再也不怕看不懂苹果代码以及api接口了
- 整理常用的iOS开源项目资源
- iOS常用动画代码
- 统计iOS项目的总代码行数的方法
- iOS项目开发常用功能静态库
- iOS 17个常用代码整理
- iOS项目统计总代码行数
- iOS 常用代码
- iOS代码常用的代码片段