iphone 常用代码

键盘上的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"]];

UIViewdrawRect方法内,用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];
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值