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:
- UIImage *image = [UIImage imageNamed:filename];
- CGImageRef imageimageRef = image.CGImage;
-
- CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
-
- CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
-
- UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
2. code used to determine whether the device is an iphone or iphone 4:
- #define isRetina ([UIScreen instancesRespondToSelector:
- @selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960),
- [[UIScreen mainScreen] currentMode].size) : NO)
3. Check whether the email address is entered correctly:
- - (BOOL) validateEmail: (NSString *) candidate {
- NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
- NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
- return [emailTest evaluateWithObject:candidate];
- }
4. How to save the current view as a photo album:
- # Import <QuartzCore/QuartzCore. h>
- UIGraphicsBeginImageContext (currentView. bounds. size); // currentView current view
- [CurrentView. layer renderInContext: UIGraphicsGetCurrentContext ()];
- UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext ();
- UIGraphicsEndImageContext ();
- 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:
- UILocalNotification * notification = [[UILocalNotification alloc] init];
- If (notification! = Nil ){
- NSLog (@ "> support local notification ");
- NSDate * now = [NSDate new];
- Notification. fireDate = [now addTimeInterval: 10];
- Notification. timeZone = [NSTimeZone defaultTimeZone];
- Notification. alertBody = @ "it's time to eat dinner! ";
- [[UIApplication sharedApplication]. scheduleLocalNotification: notification];
- }
6. Capture iphone call events:
- CTCallCenter *center = [[CTCallCenter alloc] init];
- center.callEventHandler = ^(CTCall *call)
- {
- 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:
- float rotateAngle = M_PI;
- CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);
- imageView.transform = transform;
9. Set the rotation origin:
- #import <QuartzCore/QuartzCore.h>
- UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
- imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
10. Implement custom status bar to hide the status bar ):
- CGRect frame = {{0, 0}, {320, 20}};
- UIWindow* wd = [[UIWindow alloc] initWithFrame:frame];
- [wd setBackgroundColor:[UIColor clearColor]];
- [wd setWindowLevel:UIWindowLevelStatusBar];
- frame = CGRectMake(100, 0, 30, 20);
- UIImageView* img = [[UIImageView alloc] initWithFrame:frame];
- [img setContentMode:UIViewContentModeCenter];
- [img setImage:[UIImage imageNamed:@"00_0103.png"]];
- [wd addSubview:img];
- [wd makeKeyAndVisible];
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:2];
- frame.origin.x += 150;
- [img setFrame:frame];
- [UIView commitAnimations];
11. Call in the program:
- // Add the phone icon button
- UIButton * btnPhone = [[UIButton buttonWithType: UIButtonTypeCustom] retain];
- BtnPhone. frame = CGRectMake (280,10, 30,30 );
- UIImage * image = [UIImage imageNamed: @ "phone.png"];
- [BtnPhone setBackgroundImage: image forState: UIControlStateNormal];
- // Click the dialing button to directly dial
- [BtnPhone addTarget: self action: @ selector (callAction: event :) forControlEvents: UIControlEventTouchUpInside];
- [Cell. contentView addSubview: btnPhone]; // cell is a UITableViewCell
- // Define the operation when the dial button is clicked
- -(Void) callAction :( id) sender event :( id) event {
- NSSet * touches = [event allTouches];
- UITouch * touch = [touches anyObject];
- CGPoint currentTouchPosition = [touch locationInView: self. listTable];
- NSIndexPath * indexPath = [self. listTable indexPathForRowAtPoint: currentTouchPosition];
- If (indexPath = nil ){
- Return;
- }
- NSInteger section = [indexPath section];
- NSUInteger row = [indexPath row];
- NSDictionary * rowData = [datas objectAtIndex: row];
- NSString * num = [[NSString alloc] initWithFormat: @ "tel: // % @", number]; // number indicates the number string
- [[UIApplication sharedApplication] openURL: [NSURL URLWithString: num]; // dial
- }
12. Change the iphone keyboard color:
1. Only these two types of numeric keyboards have an effect. UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad
2. keyboardAppearance = UIKeyboardAppearanceAlert
- - (void)textViewDidBeginEditing:(UITextView *)textView{
- NSArray *ws = [[UIApplication sharedApplication] windows];
- for(UIView *w in ws){
- NSArray *vs = [w subviews];
- for(UIView *v in vs)
- {
- if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"])
- {
- v.backgroundColor = [UIColor redColor];
- }
- }
- }
13. Set the time zone
- NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];
- NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT"];
- [NSTimeZone setDefaultTimeZone:tzGMT];
Use either of the preceding two time zones.
14. How to hide and trigger a keyboard on the Ipad.
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillHide:)
- name:UIKeyboardWillHideNotification
- object:nil];
- - (IBAction)keyboardWillHide:(NSNotification *)note
15. Calculate the number of characters in a string
- -(int)calculateTextNumber:(NSString *)text
- {
- float number = 0.0;
- int index = 0;
- for (index; index < [text length]; index++)
- {
- NSString *protoText = [text substringToIndex:[text length] - index];
- NSString *toChangetext = [text substringToIndex:[text length] -1 -index];
- NSString *charater;
- if ([toChangetext length]==0)
- {
- charater = protoText;
- }
- else
- {
- NSRange range = [text rangeOfString:toChangetext];
- charater = [protoText stringByReplacingCharactersInRange:range withString:@""];
- }
- NSLog(charater);
- if ([charater lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3)
- {
- number++;
- }
- else
- {
- numbernumber = number+0.5;
- }
- }
- return ceil(number);
- }
Summary:IPhone DevelopmentI hope this article will help you with some tips!