Common Code for IPhone UI development

Source: Internet
Author: User

1. display images:

 

CGRect myImageRect = CGRectMake (0.0f, 0.0f, 3200000f, 109.0f );
UIImageView * myImage = [[UIImageView alloc] initWithFrame: myImageRect];
[MyImage setImage: [UIImage imageNamed: @ "myImage.png"];
MyImage. opaque = YES;
// Whether opaque is transparent [self. view addSubview: myImage];
[MyImage release];
 

 

2. Change the background of the cell.

UIView * myview = [[UIView alloc] init];
Myview. frame = cgrecctmake (0, 0,320, 47 );
Myview. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "0006.png"];
Cell. selectedBackgroundView = myview;
3. How to convert UTF8 to unicode using NSString

Utf8Str //
NSString * unicodeStr = [NSString stringWithCString: [utf8Str UTF8String] encoding: NSUnicodeStringEncoding];
 

4. WebView:

CGRect webFrame = CGRectMake (0.0, 0.0, 320.0, 460.0 );
UIWebView * webView = [[UIWebView alloc] initWithFrame: webFrame];
[WebView setBackgroundColor: [UIColor whiteColor];
NSString * urlAddress = @ "http://www.bkjia.com ";
NSURL * url = [NSURL URLWithString: urlAddress];
NSURLRequest * requestObj = [NSURLRequest requestWithURL: url];
[WebView loadRequest: requestObj];
[Self addSubview: webView];
[WebView release];
 

5. Animation: displays a series of images one by one

NSArray * myImages = [NSArray images: [UIImage imageNamed: @ "myImage1.png"], [UIImage imageNamed: @ "myImage2.png"], [UIImage imageNamed: @ "myImage3.png"], [UIImage imageNamed: @ "myImage4.gif"], nil];
UIImageView * myAnimatedView = [UIImageView alloc];
[MyAnimatedView initWithFrame: [self bounds];
MyAnimatedView. animationImages = myImages; // The animationImages attribute returns an array of animation images.
MyAnimatedView. animationDuration = 0.25; // time used to browse the entire image
MyAnimatedView. animationRepeatCount = 0; // 0 = number of times the loops forever animation is repeated
[MyAnimatedView startAnimating];
[Self addSubview: myAnimatedView];
[MyAnimatedView release];

6. Animation: something is displayed moving on the screen. Note: This type of animation is "not processed after the start"-you cannot obtain any information about the object in the animation (such as the current position ). If you need this information, you will manually use the timer to adjust the X and Y coordinates of the animation.

Import QuartzCore. framework.

CABasicAnimation * theAnimation;
TheAnimation = [CABasicAnimation animationWithKeyPath: @ "transform. translation. x"];
// Creates and returns an CAPropertyAnimation instance for the specified key path.
// Parameter: the key path of the property to be animated
TheAnimation. duration = 1;
TheAnimation. repeatCount = 2;
TheAnimation. autoreverses = YES;
TheAnimation. fromValue = [NSNumber numberWithFloat: 0];
TheAnimation. toValue = [NSNumber numberWithFloat:-60];
[View. layer addAnimation: theAnimation forKey: @ "animateLayer"];
 

7. Draggable items // drag the project
Here's how to create a simple draggable image. // This is how to generate a simple drag image

1. Create a new class that inherits from UIImageView
@ Interface myDraggableImage: UIImageView {}
2. In the implementation for this new class, add the 2 methods:
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
// Retrieve the touch point retrieval touchpoint
CGPoint pt = [[touches anyObject] locationInView: self];
StartLocation = pt;
[[Self superview] bringSubviewToFront: self];
}
-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event
{
// Move relative to the original touch point to Move relative to the previous touch point
CGPoint pt = [[touches anyObject] locationInView: self];
CGRect frame = [self frame];
Frame. origin. x + = pt. x-startLocation. x;
Frame. origin. y + = pt. y-startLocation. y;
[Self setFrame: frame];
}
3. Now instantiate the new class as you wowould any other new image and add it to your view
// Put the new class of the instance to your view.
Dragger = [[myDraggableImage alloc] initWithFrame: myDragRect];
[Dragger setImage: [UIImage imageNamed: @ "myImage.png"];
[Dragger setUserInteractionEnabled: YES];
 

8. Plist files

Application-specific plist files can be stored in the Resources folder of the app bundle. when the app first launches, it shocould check if there is an existing plist in the user's orders ents folder, and if not it shocould copy the plist from the app bundle.

// Look in statements for an existing plist file
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
MyPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @ "% @. plist", plistName];
[MyPlistPath retain];
// If it's not there, copy it from the bundle
NSFileManager * fileManger = [NSFileManager defaultManager];
If (! [FileManger fileExistsAtPath: myPlistPath])
{
NSString * pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource: plistName ofType: @ "plist"];
}
// Now read the plist file from Documents
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectoryPath = [paths objectAtIndex: 0];
NSString * path = [documentsDirectoryPath stringByAppendingPathComponent: @ "myApp. plist"];
NSMutableDictionary * plist = [NSDictionary dictionaryWithContentsOfFile: path];
// Now read and set key/values
MyKey = (int) [[plist valueForKey: @ "myKey"] intValue];
MyKey2 = (bool) [[plist valueForKey: @ "myKey2"] boolValue];
[Plist setValue: myKey forKey: @ "myKey"];
[Plist writeToFile: path atomically: YES];

 

9. Alerts

Show a simple alert with OK button.

UIAlertView * alert = [[UIAlertView alloc] initWithTitle: nil message:
@ "An Alert! "Delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil];
[Alert show];
[Alert release];
 



From Bai Lu. China

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.