Basic knowledge of IOS development-fragment 10, basic knowledge of ios-Fragment

Source: Internet
Author: User

Basic knowledge of IOS development-fragment 10, basic knowledge of ios-Fragment

1: how to add the background effect when selecting table cell columns

if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];        cell.backgroundColor = [UIColor clearColor];        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];        cell.textLabel.textColor = [UIColor whiteColor];        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];        UIView *sbg=[[UIView alloc] initWithFrame:cell.frame];        sbg.backgroundColor=[UIColor colorWithWhite:0.5 alpha:0.3];        cell.selectedBackgroundView = sbg;    }

2: Modify the text in the title bar

UIColor *cc = [UIColor whiteColor];    NSDictionary * dict = [NSDictionary dictionaryWithObject:cc forKey:UITextAttributeTextColor];    self.navigationController.navigationBar.titleTextAttributes = dict;

3: code for a rolling start page Function

# Define NewFeatureCount 4 @ interface HVWNewFeatureViewController () <strong> @ property (nonatomic, strong) UIPageControl * pageControl; @ end @ implementation callback-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // Add scrollView [self setupScrollView]; // Add pageControl [self setupPageControl];}/** add scrollView */-(void) setup ScrollView {// create a scrollView UIScrollView * scrollView = [[UIScrollView alloc] init]; scrollView. frame = self. view. bounds; // Add an image for (int I = 0; I <NewFeatureCount; I ++) {// obtain the image NSString * featureImageName = [NSString stringWithFormat: @ "new_feature _ % d", I + 1]; UIImageView * featureImageView = [[UIImageView alloc] initWithImage: [UIImage imageWithNamed: featureImageName]; // you can specify CGFloat featureW. Idth = self. view. width; CGFloat featureHeight = self. view. height; CGFloat featureX = featureImageView. width * I; CGFloat featureY = 0; featureImageView. frame = CGRectMake (featureX, featureY, featureWidth, featureHeight); // if it is the last page, add the function button if (I = (NewFeatureCount-1 )) {// to enable the function button on the last page to take effect, the interaction feature featureImageView must be activated. userInteractionEnabled = YES; [self addFunctionButton: featureImageView];} // Add Image to scrollView [scrollView addSubview: featureImageView];} // sets scrollView function attribute scrollView. userInteractionEnabled = YES; scrollView. scrollEnabled = YES; // supports scrollView scrolling. contentSize = CGSizeMake (self. view. width * NewFeatureCount, 0); // you only need to scroll horizontally. pagingEnabled = YES; // supports paging scrollView. showsHorizontalScrollIndicator = NO; // hide the horizontal scroll bar // set the background color scrollView. backgroundColor = [UIColor colorWith Red: 246/255. 0 green: 246/255. 0 blue: 246/255. 0 alpha: 1.0]; // sets the proxy scrollView. delegate = self; // Add [self. view addSubview: scrollView];}/** add pageControl */-(void) setupPageControl {// pageControl cannot be added to scrollView, otherwise, the UIPageControl * pageControl = [[UIPageControl alloc] init]; pageControl will be rolled along with the content. pageIndicatorTintColor = [UIColor blackColor]; pageControl. currentPageIndicatorTintColor = [UIColor redCo Lor]; pageControl. numberOfPages = NewFeatureCount; // you can specify the position pageControl. centerX = self. view. width * 0.5; pageControl. centerY = self. view. height * 0.9; self. pageControl = pageControl; [self. view addSubview: pageControl] ;}# pragma mark-UIScrollViewDelegate/** scrollView scroll proxy method, where the page number indicator */-(void) scrollViewDidScroll :( UIScrollView *) scrollView {// the page number is changed when the image is rolled over the midline. pageControl. currentPage = ScrollView. contentOffset. x/scrollView. width + 0.5;} # features on the last page of The pragma mark/** add function button */-(void) addFunctionButton :( UIImageView *) imageView {// Add the "share" Option Button [self addShareButton: imageView]; // Add the "Enter Weibo" button [self addEnterWeiboButton: imageView];} /** Share Option Button */-(void) addShareButton :( UIImageView *) imageView {// create button UIButton * shareButton = [UIButton buttonWithType: UIButtonTypeCustom]; [sharebutse TTitle: @ "share with you" forState: UIControlStateNormal]; [shareButton setImage: [UIImage imageWithNamed: @ "success"] forState: Success]; [shareButton setImage: [UIImage imageWithNamed: @ "success"] forState: UIControlStateSelected]; [shareButton addTarget: self action: @ selector (shareButtonClicked :) forControlEvents: UIControlEventTouchUpInside]; [shareButton setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; // The shareButton position. size = CGSizeMake (150, 50); // you must first set the size so that the center is actually in the center. Otherwise, it starts from the upper left corner !!! ShareButton. centerX = self. view. width * 0.5; shareButton. centerY = self. view. height * 0.65; // set the internal spacing shareButton. titleEdgeInsets = UIEdgeInsetsMake (0, 10.0, 0, 0); // Add [imageView addSubview: shareButton];}/** click event Method in sharing options */-(void) shareButtonClicked :( UIButton *) button {button. selected =! Button. selected;}/** "Enter Weibo" button */-(void) addEnterWeiboButton :( UIImageView *) imageView {// create button UIButton * enterButton = [UIButton buttonWithType: UIButtonTypeCustom]; enterButton. userInteractionEnabled = YES; [enterButton setBackgroundImage: [UIImage imageWithNamed: @ "success"] forState: UIControlStateNormal]; [enterButton failed: [UIImage imageWithNamed: @ "success"] forState: Success]; [enterButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; [enterButton setTitle: @ "" forState: UIControlStateNormal]; // The position and size of enterButton. size = enterButton. currentBackgroundImage. size; enterButton. centerX = self. view. width * 0.5; enterButton. centerY = self. view. height * 0.8; // click [enterButton addTarget: self action: @ selector (enterWeiboButtonClicked) forControlEvents: UIControlEventTouchUpInside] For listening; // Add [imageView addSubview: enterButton];} /** click the "Enter Weibo" button */-(void) enterWeiboButtonClicked {UIWindow * window = [UIApplication sharedApplication]. keyWindow; window. rootViewController = [[HVWTabBarViewController alloc] init];} @ end

4: added the delete controller.

Add a controller: [self addChildViewController: toViewController]; [toViewController didMoveToParentViewController: self]; small instance:-(IBAction) btnAction :( id) sender {CiderViewController * cid = [[CiderViewController alloc] init]; [self addChildViewController: cid]; CGRect frame = self. myView. bounds; frame. origin. y = 110; frame. size. width = 290; frame. size. height = 90; cid. view. frame = frame; cid. view. backgroundColor = [UIColor redCol Or]; [self. myView addSubview: cid. view]; [cid didMoveToParentViewController: self];} Delete Controller: 1. when we call the removeFromParentViewController method to our view controller container, we must first call this method, and the parent parameter is nil: [View Controller willMoveToParentViewController: nil]; [fromViewController willMoveToParentViewController: nil]; [fromViewController removeFromParentViewController]; Í Description: Use of willMoveToParentViewController and didMoveToParentViewController 1. these two This method is called when the sub-controller tries to switch! That is, it is called when the transitionFromViewController method is called. 2. When you call the willMoveToParentViewController method or didMoveToParentViewController method, note that their parameters are used: When a subview controller is deleted from the parent View Controller, the parent parameter is nil. That is, [the subattempt controller willMoveToParentViewController: nil] to be deleted; When a subattempt controller is added to the parent View Controller, the parent parameter is the parent View Controller. That is: [didMoveToParentViewController: parent View Controller]; 3. You do not need to call the [subview controller willMoveToParentViewController: parent View Controller] method. Because the [Parent View Controller addChildViewController: subview controller] has been called by default. You only need to call the [subview controller didMoveToParentViewController: parent View Controller] After the transitionFromViewController method. 4. You do not need to call the [subview controller didMoveToParentViewController: parent View Controller] method. This is because the [subview controller removeFromParentViewController] has been called by default. You only need to call [subview controller willMoveToParentViewController: nil] before the transitionFromViewController method. Good article (http://www.cocoanetics.com/2012/04/containing-viewcontrollers)

5: Research on the autoresizingMask attribute of UIView

In UIView, there is an autoresizingMask attribute, which corresponds to an enumerated value (as follows). The attribute means to automatically adjust the position between the child control and the parent control, with a width and height. Enum {UIViewAutoresizingNone = 0, expires = 1 <0, UIViewAutoresizingFlexibleWidth = 1 <1, expires = 1 <2, UIViewAutoresizingFlexibleTopMargin = 1 <3, UIViewAutoresizingFlexibleHeight = 1 <4, UIViewAutoresizingFlexibleBottomMargin = 1 <5}; UIViewAutoresizingNone is not automatically adjusted. UIViewAutoresizingFlexibleLeftMargin automatically adjusts the distance from the left side of superView to ensure that the distance from the right side of superView remains unchanged. UIViewAutoresizingFlexibleRightMargin automatically adjusts the right distance from the superView to ensure that the left distance from the superView remains unchanged. UIViewAutoresizingFlexibleTopMargin automatically adjusts the distance from the superView top to ensure that the distance from the superView bottom remains unchanged. UIViewAutoresizingFlexibleBottomMargin automatically adjusts the distance from the superView bottom, that is, the distance from the superView top remains unchanged. UIViewAutoresizingFlexibleWidth automatically adjusts its width to ensure the distance from the left and right sides of superView. UIViewAutoresizingFlexibleHeight automatically adjusts its height to ensure the distance from the top and bottom of the superView. UIViewAutoresizingFlexibleLeftMargin. For example, if the original distance is 20, 30, the adjusted distance should be 68,102, that is, 68/20 = 102/30. Other combinations are similar. Instance: CGRect frame = [[UIScreen mainScreen] applicationFrame]; UIView * view = [[UIView alloc] initWithFrame: frame]; view. autoresizingMask = custom | UIViewAutoresizingFlexibleHeight;

6: The dispatch method provided by the system

To facilitate GCD usage, Apple provides some methods to facilitate block execution in the main thread or background thread, or delay execution. Example: 1 // background execution: 2 dispatch_async (dispatch_get_global_queue (0, 0), ^ {3 // something 4}); 5 // main thread execution: 6 dispatch_async (dispatch_get_main_queue (), ^ {7 // something 8}); 9 // One-time execution: 10 static dispatch_once_t onceToken; 11 dispatch_once (& onceToken, ^ {12 // code to be executed once 13}); 14 // 2 seconds delayed execution: 15 double delayInSeconds = 2.0; 16 dispatch_time_t popTime = dispatch_time (DISPATCH_TIME_NOW, DelayInSeconds * NSEC_PER_SEC); 17 dispatch_after (popTime, dispatch_get_main_queue (), ^ (void) {18 // code to be executed on the main queue after delay 19 }); the queue can also be customized. to customize the queue, you can use the dispatch_queue_create method. For example: 1 hour urls_queue = dispatch_queue_create ("queue", NULL); 2 dispatch_async (urls_queue, ^ {3 // your code 4}); 5 dispatch_release (urls_queue); in addition, GCD There are also some advanced usage, such as letting two threads in the background execute in parallel, and then summarizing the execution results after both threads are finished. This can be implemented using dispatch_group, dispatch_group_async, and dispatch_group_notify. Example: 1 dispatch_group_t group = dispatch_group_create (); 2 dispatch_group_async (group, limit ), ^ {3 // parallel execution thread 4}); 5 dispatch_group_async (group, dispatch_get_global_queue (), ^ {6 // parallel execution thread 2 7 }); 8 dispatch_group_notify (group, dispatch_get_global_queue (0, 0), ^ {9 // summary result 10 });

 

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.