iphone常用代码锦集(二)

时间:2023-01-23 00:28:36

iphone常用代码锦集(二)详细内容

OpenGL 及 着色器语言 入门 详

iphone常用代码锦集(二)详细内容

OpenGL教程三 增加投影和简单

iphone常用代码锦集(二)详细内容

OpenGL教程二 添加着色器(sha

现在的位置: 首页 >技术 >Iphone > Iphone技巧 > 正文RSS 上篇下篇 iphone常用代码锦集(二)2012年02月16日Iphone技巧⁄ 共 7899字 评论数 1

键盘上的return键改成Done: textField.returnKeyType = UIReturnKeyDone;

textfield设置成为密码框:   [textField_pwd setSecureTextEntry:YES];

收回键盘  [textField   resignFirstResponder];    或者  [textfield addTarget:self action:@selector(textFieldDone:)  forControlEvents:UIControlEventEditingDidEndOnExit];

振动: 

#import <AudioToolbox/AudioToolbox.h>   //需加头文件

方法一:     AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

方法二:    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

当设备不支持方法一函数时,起蜂鸣作用,  而方法二支持所有设备

用Cocoa删除文件:

NSFileManager *defaultManager = [NSFileManager defaultManager]; 

[defaultManager removeFileAtPath: tildeFilename   handler: nil];

UIView透明渐变与移动效果:

//动画配制开始  

[UIView beginAnimations:@"animation" context:nil]; 

[UIView setAnimationDuration:2.5]; 

//图片上升动画  

CGRect rect = imgView.frame ; 

rect.origin.y = 30; 

imgView.frame = rect; 

//半透明度渐变动画  

imgView.alpha = 0; 

//提交动画  

[UIView commitAnimations];

 

用HTTP协议,获取www.baidu.com网站的HTML数据:

[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"]];

在UIView的drawRect方法内,用Quartz2D API绘制一个像素宽的水平直线:

-(void)drawRect:(CGRect)rect{ 

  //获取图形上下文     

  CGContextRef context = UIGraphicsGetCurrentContext(); 

  //设置图形上下文的路径绘制颜色

  CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 

  //取消防锯齿

  CGContextSetAllowsAntialiasing(context, NO); 

  //添加线

  CGContextMoveToPoint(context, 50, 50);    

  CGContextAddLineToPoint(context, 100, 50); 

  //绘制

  CGContextDrawPath(context, kCGPathStroke); 

}

用UIWebView加载: www.baidu.com

UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

[web loadRequest:[NSURLRequest requestWithURL:[NSURL  URLWithString:@"http://www.baidu.com"]]];

[self.view addSubview:web];

[web release];

用NSTimer做一个定时器,每隔1秒执行一次 pressedDone;

-(IBAction)clickBtn:(id)sender{ 

   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1   target:self selector:@selector(printHello) userInfo:nil  repeats:YES]; [timer fire];  }

                             target:self

                             selector:@selector(pressedDone)

                 userInfo:nil

         repeats:YES];

    [timer fire]; 

}

 

利用UIImageView实现动画:

- (void)viewDidLoad {
      [super viewDidLoad];

       UIImageView *fishAni=[[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       [self.view addSubview:fishAni];
       [fishAni release];

       //设置动画帧
       fishAni.animationImages=[NSArray arrayWithObjects:
       [UIImage imageNamed:@"1.jpg"],
       [UIImage imageNamed:@"2.jpg"],
       [UIImage imageNamed:@"3.jpg"],
       [UIImage imageNamed:@"4.jpg"],
       [UIImage imageNamed:@"5.jpg"],nil ];

       //设置动画总时间
        fishAni.animationDuration=1.0;

       //设置重复次数,0表示不重复
       fishAni.animationRepeatCount=0;

       //开始动画
       [fishAni startAnimating];    
}

利用苹果机里的相机进行录像:

-(void) choosePhotoBySourceType: (UIImagePickerControllerCameraCaptureMode) sourceType
{
   m_imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
   m_imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   m_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
   m_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
   //m_imagePickerController.cameraCaptureMode =   UIImagePickerControllerCameraCaptureModeVideo;
   
   NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:m_imagePickerController.sourceType];
   if ([sourceTypes containsObject:(NSString *)kUTTypeMovie ])
   {
       m_imagePickerController.mediaTypes= [NSArray arrayWithObjects:(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage,nil];
      
   
  // m_imagePickerController.cameraCaptureMode = sourceType;
   //m_imagePickerController.mediaTypes
   //imagePickerController.allowsEditing = YES;
   
   [self presentModalViewController: m_imagePickerController animated:YES];

}

-(void) takePhoto
{
   if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
   {
       [self choosePhotoBySourceType:nil];
   }
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   [super viewDidLoad];
   
   UIButton *takePhoto = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   [takePhoto setTitle:@"录像" forState:UIControlStateNormal];
   [takePhoto addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
   takePhoto.frame = CGRectMake(50,100,100,30);
   [self.view addSubview:takePhoto];
}

在UIImageView中旋转图像:

float rotateAngle M_PI;  //M_PI为一角度 

CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);   

imageView.transform transform;

隐藏状态栏:

方法一, [UIApplication  sharedApplication] setStatusBarHidden:YES animated:NO];

方法二, 在应用程序的Info.plist 文件中将 UIStatusBarHidden 设置为YES;

构建多个可拖动视图:

@interface DragView: UIImageView{

   CGPoint  startLocation;

   NSString  *whichFlower;

}

@property (nonatomic ,retain)NSString *whichFlower;

@end

@implementation DragView

@synthesize whichFlower;

   (void) touchesBegan:(NSSet *)touches withEvent: (UIEvent *)event{

   CGPoint pt = [[touhes anyObject ] locationInView:self];

   startLocation = pt;

   [[self superview] bringSubviewToFront:self];

}

   (void) touchesMoved:(NSSet *)touches withEvent:( UIEvent *)event{

    CGPoint pt = [[touches anyObject] locatonInView:self];

    CGRect frame = [self frame];

    frame.origin.x += pt.x – startLocation.x;

    frame.origin.y += pt.y -  startLocation.y;

    [self  setFrame:frame];

}

@end

@interface TestController :UIViewController{

     UIView  *contentView;

}

@end

@implementation TestController

#define MAXFLOWERS 16

CGPoint randomPoint(){ return CGPointMake(random()%6 , random()96);}

   (void)loadView{

    CGRect apprect = [[UIScreen mainScreen] applicationFrame];

    contentView = [[UIView alloc] initWithFrame :apprect];

    contentView.backgroundColor = [UIColor blackColor];

    self.view = contentView;

    [contentView  release];

    for(int i=0 ; i<MAXFLOWERS; i++){

        CGRect dragRect = CGRectMake(0.0f ,0.0f, 64.0f ,64.0f);

        dragRect.origin = randomPoint();

        DragView *dragger = [[DragView alloc] initWithFrame:dragRect];

        [dragger setUserInteractionEnabled: YES];

        NSString *whichFlower = [[NSArray arrayWithObjects:@”blueFlower.png”,@”pinkFlower.png”,nil] objectAtIndex:( random() %2)];

        [dragger setImage:[UIImage imageNamed:whichFlower]];

        [contentView   addSubview :dragger];

        [dragger  release];

    }

}

   (void)dealloc{

     [contentView release];

     [super dealloc];

}

@end

隐藏导航栏:

    self.navigationController.navigationBarHidden = YES;

定时器timer的使用:

    NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES]retain];

在数字键盘上添加button:
//定义一个消息中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

//addObserver:注册一个观察员 name:消息名称
- (void)keyboardWillShow:(NSNotification *)note {
   // create custom button
   UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
   doneButton.frame = CGRectMake(0, 163, 106, 53);
   [doneButton setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
   [doneButton addTarget:self action:@selector(addRadixPoint) forControlEvents:UIControlEventTouchUpInside];
  
   // locate keyboard view
   UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//返回应用程序window
   UIView* keyboard;
   for(int i=0; i<[tempWindow.subviews count]; i++) //遍历window上的所有subview
   {
       keyboard = [tempWindow.subviews objectAtIndex:i];
       // keyboard view found; add the custom button to it
       if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
       [keyboard addSubview:doneButton];
   }
}

View自己调用自己的方法:
[self performSelector:@selector(loginToNext) withObject:nil afterDelay:2];//黄色段为方法名,和延迟几秒执行.

Alerts 警告:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil

                                          message:@"An Alert!"

                                          delegate:self

                                          cancelButtonTitle:@"OK"

                                          otherButtonTitles:nil];
[alert show];
[alert release];

将一个控件放在视图之上:
[scrollView insertSubview:searchButton aboveSubview:scrollView];

返回
【上篇】经常用电脑的人士注意喽
【下篇】特色幽默

您可能还会对这些文章感兴趣!

  1. 打开应用的方法
  2. 用scroll view 的 bounds 实现 scroll view 平滑滚动到目标
  3. Cocoa 正则表达式使用小例
  4. 内存哗哗地往上涨
  5. ios开发随笔-误人子弟
  6. iphone UIView layer frame transform 动画细节
  7. iphone 图片四周不拉伸的方法
  8. iphone 如何在程序中判断当前的环境是中文还是英文?
iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)

目前有1 条留言 其中:访客:1 条, 博主:0 条

  1. iphone常用代码锦集(二)criminal background check california department justice :2013年05月15日23:10:32 -49楼 @回复回复

    Should this particular burglar alarm haven’t your BBN crew, collaborated Jack port destinations stability fresh vegetables; moreover it enable you to talk about your info, cred

给我留言

iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)iphone常用代码锦集(二)

插入图片

随便看看

文章足迹

2013 年七月
« 一    
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31  

常用标签

androidAndroid学习笔记cellCGContextCGContextRefGroupediOSiPadIphonejavajsNSCFStringNSStringOpenGLOpenGL入门OpenGL教程oraceloracleSEO优化setNeedsDisplaysetNeedsLayouttableviewthis class is not key value coding-compliant for the keyUIButtonUITableViewUIView不同代码内存区别基本概念学习学习笔记对比常用常用代码总结技巧特殊符号用法着色器编程解决运行流程锦集

推荐栏目

订阅本站

iphone常用代码锦集(二)

同分类最新文章

最活跃的读者

  • iphone常用代码锦集(二)
  • iphone常用代码锦集(二)
  • iphone常用代码锦集(二)

网站统计

    日志:138篇
    评论:274条
    分类:17个
    标签:250个
    链接:9个
    网站运行:738天
最后更新:2013年1月29日

返回首页

Copyright © 2011-2013 不二过  保留所有权利.  Theme by Robin