Tips for iPhone Development

Source: Internet
Author: User
Tags time zones

IPhone DevelopmentSome tips are introduced in this article. I will share an article useful to programming friends with you. Let's take a look at the content.

After more than half a yearIphone Development, I found thatDevelopmentThe most difficult part in the process is some noisy details, and learning some tips will get twice the result with half the effort. I will summarize the following:Iphone DevelopmentTips.

1. Use the following code to process an image in a program:

 
 
  1. UIImage *image = [UIImage imageNamed:filename];  
  2. CGImageRef imageimageRef = image.CGImage;  
  3.  
  4. CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);  
  5.  
  6. CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);  
  7.  
  8. UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect]; 

2. code used to determine whether the device is an iphone or iphone 4:

 
 
  1. #define isRetina ([UIScreen instancesRespondToSelector:  
  2. @selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960),   
  3. [[UIScreen mainScreen] currentMode].size) : NO) 

3. Check whether the email address is entered correctly:

 
 
  1. - (BOOL) validateEmail: (NSString *) candidate {  
  2. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";   
  3. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];   
  4. return [emailTest evaluateWithObject:candidate];  

4. How to save the current view as a photo album:

 
 
  1. # Import <QuartzCore/QuartzCore. h>
  2. UIGraphicsBeginImageContext (currentView. bounds. size); // currentView current view
  3. [CurrentView. layer renderInContext: UIGraphicsGetCurrentContext ()];
  4. UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext ();
  5. UIGraphicsEndImageContext ();
  6. UIImageWriteToSavedPhotosAlbum (viewImage, nil );

5. A local notification is similar to a push notification. Press the home Key to go to the background for 10 seconds:

 
 
  1. UILocalNotification * notification = [[UILocalNotification alloc] init];
  2. If (notification! = Nil ){
  3. NSLog (@ "> support local notification ");
  4. NSDate * now = [NSDate new];
  5. Notification. fireDate = [now addTimeInterval: 10];
  6. Notification. timeZone = [NSTimeZone defaultTimeZone];
  7. Notification. alertBody = @ "it's time to eat dinner! ";
  8. [[UIApplication sharedApplication]. scheduleLocalNotification: notification];
  9. }

6. Capture iphone call events:

 
 
  1. CTCallCenter *center = [[CTCallCenter alloc] init];  
  2. center.callEventHandler = ^(CTCall *call)   
  3. {  
  4. NSLog(@"call:%@", call.callState);  

7. iOS 4 introduces multi-task support, so after you press the "Home" key, the program may not exit but be transferred to the background for running. If you want the Application to exit directly, the simplest method is to find the Application does not run in background option in info-plist, and select this option.

8. Rotate the image of UIimageView:

 
 
  1. float rotateAngle = M_PI;  
  2. CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);  
  3. imageView.transform = transform; 

9. Set the rotation origin:

 
 
  1. #import <QuartzCore/QuartzCore.h> 
  2. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];  
  3. imageView.layer.anchorPoint = CGPointMake(0.5, 1.0); 

10. Implement custom status bar to hide the status bar ):

 
 
  1. CGRect frame = {{0, 0}, {320, 20}};  
  2. UIWindow* wd = [[UIWindow alloc] initWithFrame:frame];  
  3. [wd setBackgroundColor:[UIColor clearColor]];  
  4. [wd setWindowLevel:UIWindowLevelStatusBar];  
  5. frame = CGRectMake(100, 0, 30, 20);  
  6. UIImageView* img = [[UIImageView alloc] initWithFrame:frame];  
  7. [img setContentMode:UIViewContentModeCenter];  
  8. [img setImage:[UIImage imageNamed:@"00_0103.png"]];  
  9. [wd addSubview:img];  
  10. [wd makeKeyAndVisible];  
  11. [UIView beginAnimations:nil context:nil];  
  12. [UIView setAnimationDuration:2];  
  13. frame.origin.x += 150;  
  14. [img setFrame:frame];  
  15. [UIView commitAnimations]; 

11. Call in the program:

 
 
  1. // Add the phone icon button
  2. UIButton * btnPhone = [[UIButton buttonWithType: UIButtonTypeCustom] retain];
  3. BtnPhone. frame = CGRectMake (280,10, 30,30 );
  4. UIImage * image = [UIImage imageNamed: @ "phone.png"];
  5. [BtnPhone setBackgroundImage: image forState: UIControlStateNormal];
  6. // Click the dialing button to directly dial
  7. [BtnPhone addTarget: self action: @ selector (callAction: event :) forControlEvents: UIControlEventTouchUpInside];
  8. [Cell. contentView addSubview: btnPhone]; // cell is a UITableViewCell
  9. // Define the operation when the dial button is clicked
  10. -(Void) callAction :( id) sender event :( id) event {
  11. NSSet * touches = [event allTouches];
  12. UITouch * touch = [touches anyObject];
  13. CGPoint currentTouchPosition = [touch locationInView: self. listTable];
  14. NSIndexPath * indexPath = [self. listTable indexPathForRowAtPoint: currentTouchPosition];
  15. If (indexPath = nil ){
  16. Return;
  17. }
  18. NSInteger section = [indexPath section];
  19. NSUInteger row = [indexPath row];
  20. NSDictionary * rowData = [datas objectAtIndex: row];
  21. NSString * num = [[NSString alloc] initWithFormat: @ "tel: // % @", number]; // number indicates the number string
  22. [[UIApplication sharedApplication] openURL: [NSURL URLWithString: num]; // dial
  23. }

12. Change the iphone keyboard color:

1. Only these two types of numeric keyboards have an effect. UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad

2. keyboardAppearance = UIKeyboardAppearanceAlert

 
 
  1. - (void)textViewDidBeginEditing:(UITextView *)textView{  
  2. NSArray *ws = [[UIApplication sharedApplication] windows];  
  3. for(UIView *w in ws){  
  4. NSArray *vs = [w subviews];  
  5. for(UIView *v in vs)  
  6. {  
  7. if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"])  
  8. {  
  9. v.backgroundColor = [UIColor redColor];  
  10. }  
  11. }  

13. Set the time zone

 
 
  1. NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];  
  2. NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT"];  
  3. [NSTimeZone setDefaultTimeZone:tzGMT]; 

Use either of the preceding two time zones.

14. How to hide and trigger a keyboard on the Ipad.

 
 
  1. [[NSNotificationCenter defaultCenter] addObserver:self  
  2. selector:@selector(keyboardWillHide:)  
  3. name:UIKeyboardWillHideNotification  
  4.   object:nil];  
  5. - (IBAction)keyboardWillHide:(NSNotification *)note 

15. Calculate the number of characters in a string

 
 
  1. -(int)calculateTextNumber:(NSString *)text  
  2. {  
  3. float number = 0.0;  
  4. int index = 0;  
  5. for (index; index < [text length]; index++)  
  6. {  
  7. NSString *protoText = [text substringToIndex:[text length] - index];  
  8. NSString *toChangetext = [text substringToIndex:[text length] -1 -index];  
  9. NSString *charater;  
  10. if ([toChangetext length]==0)  
  11. {  
  12. charater = protoText;  
  13. }  
  14. else   
  15. {  
  16. NSRange range = [text rangeOfString:toChangetext];  
  17. charater = [protoText stringByReplacingCharactersInRange:range withString:@""];  
  18. }  
  19. NSLog(charater);  
  20. if ([charater lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3)  
  21. {  
  22. number++;  
  23. }  
  24. else   
  25. {  
  26. numbernumber = number+0.5;  
  27. }  
  28. }  
  29. return ceil(number);  
  30. }  

Summary:IPhone DevelopmentI hope this article will help you with some tips!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.