键盘上的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;
构建多个可拖动视图:
1 @interface DragView: UIImageView{ 2 3 CGPoint startLocation; 4 5 NSString *whichFlower; 6 7 } 8 9 @property (nonatomic ,retain)NSString *whichFlower; 10 11 @end 12 13 @implementation DragView 14 15 @synthesize whichFlower; 16 17 - (void) touchesBegan:(NSSet *)touches withEvent: (UIEvent *)event{ 18 19 CGPoint pt = [[touhes anyObject ] locationInView:self]; 20 21 startLocation = pt; 22 23 [[self superview] bringSubviewToFront:self]; 24 25 } 26 27 - (void) touchesMoved:(NSSet *)touches withEvent:( UIEvent *)event{ 28 29 CGPoint pt = [[touches anyObject] locatonInView:self]; 30 31 CGRect frame = [self frame]; 32 33 frame.origin.x += pt.x – startLocation.x; 34 35 frame.origin.y += pt.y - startLocation.y; 36 37 [self setFrame:frame]; 38 39 } 40 41 @end 42 43 @interface TestController :UIViewController{ 44 45 UIView *contentView; 46 47 } 48 49 @end 50 51 @implementation TestController 52 53 #define MAXFLOWERS 16 54 55 CGPoint randomPoint(){ return CGPointMake(random()%6 , random()96);} 56 57 - (void)loadView{ 58 59 CGRect apprect = [[UIScreen mainScreen] applicationFrame]; 60 61 contentView = [[UIView alloc] initWithFrame :apprect]; 62 63 contentView.backgroundColor = [UIColor blackColor]; 64 65 self.view = contentView; 66 67 [contentView release]; 68 69 for(int i=0 ; i<MAXFLOWERS; i++){ 70 71 CGRect dragRect = CGRectMake(0.0f ,0.0f, 64.0f ,64.0f); 72 73 dragRect.origin = randomPoint(); 74 75 DragView *dragger = [[DragView alloc] initWithFrame:dragRect]; 76 77 [dragger setUserInteractionEnabled: YES]; 78 79 NSString *whichFlower = [[NSArray arrayWithObjects:@”blueFlower.png”,@”pinkFlower.png”,nil] objectAtIndex:( random() %2)]; 80 81 [dragger setImage:[UIImage imageNamed:whichFlower]]; 82 83 [contentView addSubview :dragger]; 84 85 [dragger release]; 86 87 } 88 89 } 90 91 - (void)dealloc{ 92 93 [contentView release]; 94 95 [super dealloc]; 96 97 } 98 99 @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];