A summary of iphone development tips
1, if you want to deal with a picture in the program (get a part of a picture) can use the following code:Uiimage*image = [Uiimageimagenamed:filename];
Cgimageref imageref =image. Cgimage;
CGRect rect= CGRectMake (ORIGIN.X,ORIGIN.Y, size.width,size.height);
Cgimageref imagerefrect= Cgimagecreatewithimageinrect (imageref,rect);
UIImage *imagerect = [[Uiimagealloc] initwithcgimage:imagerefrect];
2, to determine whether the device is the iphone or iphone4 code:
#define Isretina ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 960), [[UIScreen Mainscreen] currentmode].size): NO)
3, determine whether the mailbox input is correct:
-(BOOL) Validateemail: (NSString *) candidate {
nsstring*emailregex= @ "[a-z0-9a-z._%+-]+@[a-za-z0-9.-]+\\. [A-za-z] {2,4} ";
Nspredicate *emailtest= [nspredicatepredicatewithformat:@ "Selfmatches%@", Emailregex];
return [Emailtest evaluatewithobject:candidate];
}
4, how to save the current view as a photo album to:
#import <QuartzCore/QuartzCore.h>
Uigraphicsbeginimagecontext (currentView.bounds.size); CurrentView the current View
[Currentview.layer Renderincontext:uigraphicsgetcurrentcontext ()];
UIImage *viewimage= Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Uiimagewritetosavedphotosalbum (viewimage, nil, nil, nil);
5, local notification (similar to push notification) press the home key to the background 10 seconds after the trigger:
UILOCALNOTIFICATION*NOTIFICATION=[[UILOCALNOTIFICATIONALLOC] init];
if (Notification!=nil) {
NSLog (@ ">>support local notification");
NSDate *now=[nsdatenew];
Notification.firedate=[now Addtimeinterval:10];
Notification.timezone=[nstimezonedefaulttimezone];
notification.alertbody=@ "It's time to have dinner." ";
[[UIApplication sharedapplication].schedulelocalnotification:notification];
}
6, capture the iphone call event:
Ctcallcenter *center =[[ctcallcenter alloc] init];
Center.calleventhandler = ^ (Ctcall*call)
{
NSLog (@ "call:%@", call.callstate);
}
7, IOS4 introduced multitasking support, so the user pressed the "home" key after the program may not quit but into the background run. If you want the application to exit directly, the easiest way is to find applicationdoes not run Inbackground in Info-plist, and tick.
8, so that the Uiimageview image rotation:
float rotateangle= M_pi;
Cgaffinetransform transform=cgaffinetransformmakerotation (Rotateangle);
Imageview.transform =transform;
9, set the rotation of the original point:
#import <QuartzCore/QuartzCore.h>
Uiimageview *imageview =[[uiimageviewalloc] initwithimage:[uiimageimagenamed:@ "Bg.png"];
Imageview.layer.anchorpoint= Cgpointmake (0.5, 1.0);
10, implementation of the custom status bar (Mask status bar):
CGRect frame = {0, 0},{320, 20}};
uiwindow* WD = [[Uiwindowalloc] initwithframe:frame];
[WD Setbackgroundcolor:[uicolor Clearcolor]];
[Wdsetwindowlevel:uiwindowlevelstatusbar];
Frame= CGRectMake (100, 0, 30, 20);
uiimageview* img = [[Uiimageviewalloc] 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, in the program to achieve the call:
Add Phone icon button
UIButton *btnphone =[[uibutton Buttonwithtype:uibuttontypecustom] retain];
Btnphone.frame= CGRectMake (280,10,30,30);
UIImage *image = [uiimageimagenamed:@ "Phone.png"];
[Btnphone setbackgroundimage:image Forstate:uicontrolstatenormal];
Click the Dial button to dial directly
[Btnphone addtarget:self Action: @selector (callaction:event:) forcontrolevents:uicontroleventtouchupinside];
[Cell.contentViewaddSubview:btnPhone]; The cell is a UITableViewCell
Define the action when you click the Dial button
-(void) Callaction: (ID) senderevent: (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= [[Nsstringalloc] initwithformat:@ "tel://%@", number]; Number is a string of numbers
[[UIApplication sharedapplication] Openurl:[nsurl urlwithstring:num]];//dialing
}
12, change the iphone's keyboard color:
1. Only these 2 kinds of digital keyboards are effective. 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 Time zone
Nstimezone *defaulttimezone= [Nstimezonedefaulttimezone];
Nstimezone *tzgmt =[nstimezone timezonewithname:@ "GMT"];
[Nstimezone SETDEFAULTTIMEZONE:TZGMT];
The above two time zones are arbitrary with one.
14, the ipad hidden keyboard at the same time trigger method.
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardwillhide:)
Name:uikeyboardwillhidenotification
Object:nil];
-(Ibaction) Keyboardwillhide: (nsnotification *) Note
14. The method of opening another program in one program.
Http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
15, the number of words to calculate the 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
{
Number = number+0.5;
}
}
Returnceil (number);
}
More iphone Development Summary