IOS10 A practical trick (there's always something you don't know and you'll use)

Source: Internet
Author: User

this article reprinted to http://www.jianshu.com/p/a3156826c27cin the development process we always encounter a variety of small problems, some small problems are not very easy to solve. Here I summarize the various small problems I have encountered in the development, as well as my solution. More common I will not mention, here is mainly about something you may not know (of course, it is possible that you know that the great God does not have to look down)1. Local fillet problem of control

Have you ever had a problem with a button or a label, as long as the two corners of the right corner are rounded, or just a rounded corner. What to do about it. This will require a layer mask to help us.

    CGRect rect =CGRectMake (0,0,100,50);Cgsize Radio =Cgsizemake (5,5); //fillet size uirectcorner corner =  Uirectcornertopleft| uirectcornertopright; //this fillet position uibezierpath *path = [ Uibezierpath bezierpathwithroundedrect:rect Byroundingcorners:corner Cornerradii:radio]; cashapelayer *masklayer = [[cashapelayer Alloc]init];//create Shapelayer masklayer.frame = button .bounds; Masklayer.path = Path //set path Button.layer.mask = Masklayer                

For example, a button, other controls that inherit from UIView can

2, Navigationbar of transparency issues

If you just set the alpha of Navigationbar to 0, it would be tantamount to hiding the navigationbar, and as you all know, the parent view's alpha is set to 0, then the child views are all transparent. Then the corresponding Navigationbar title and the left and right two buttons will disappear. This obviously does not reach the effect we demand.
(1) If you just want to navigationbar transparent, the buttons and headings are available in the following ways:

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];//给navigationBar设置一个空的背景图片即可实现透明,而且标题按钮都在

Careful you will find a line like this:


Img_0025.png


This requires us to do further processing, the line is removed, the following methods can be:

self.navigationController.navigationBar.shadowImage = [UIImage new];//其实这个线也是image控制的。设为空即可

(2) If you want to achieve on a transparent basis, depending on the pull-off distance, the transparency becomes opaque, then the above one seems to be out of reach, which requires us to use another method

//navigationBar是一个复合视图,它是有许多个控件组成的,那么我们就可以从他的内部入手[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//这里可以根据scrollView的偏移量来设置alpha就实现了渐变透明的效果
3. Global Settings Navigationbar heading style and Baritem heading styles
Uicolorwithhexrgb () This method is defined by itself, it just needs to give a color [[Uinavigationbar appearance] Setbartintcolor:Uicolorwithhexrgb (0xfefefe)]; [[Uinavigationbar appearance] settitletextattributes:@{nsfontattributename:[uifont boldsystemfontofsize:[],nsforegroundcolorattributename:uicolorwithhexrgb (0xfe6d27)} ]; [[Uitabbaritem appearance] settitletextattributes:@{nsfontattributename: [uifont boldsystemfontofsize: ,nsforegroundcolorattributename: uicolorwithhexrgb (0x666666)} forstate:  UIControlStateNormal]; [[Uitabbaritem appearance] settitletextattributes:@{nsfontattributename: [Uifont Boldsystemfontofsiz               
4, Navigationbar hidden display of excessive

Believe in the use of must have encountered, one page hidden Navigationbar, another does not hide. Two pages for push and pop, especially when there is a slide gesture return, do not do processing will cause the slide return, the Navigationbar position is empty, directly display a black or display the following layer of view, it is difficult to see. This requires us to add excessive animations to hide or show Navigationbar:
The page that will appear after the return implementation Viewwillappear method, need to hide is set to Yes, need to display is set to No

- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [self.navigationController setNavigationBarHidden:NO animated:YES];}
5. Slide gesture Return

The iOS slide-back gesture has a good operating experience, and apps that don't support slide-back are definitely not good apps. However, in the development process in the custom back button, or some Webview,tableview and other pages, side-slip return gesture invalidation, this time we need to set up, you can negotiate the following code in the base class:

    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {//需要遵循一下手势的代理 self.navigationController.interactivePopGestureRecognizer.delegate = self; self.navigationController.interactivePopGestureRecognizer.enabled = YES; }

Problem: When returning the top-level controller of Navigationcontroller. Slide again, this time you click on a push page operation, you will find that the card, half a day will have a reaction.
This is because the controller gesture at the top level is still valid, but after sliding, the returned page cannot be found. Cause the software to stutter, suspended animation so it is necessary in the rootviewcontroller to make this gesture invalid. Set the following to No

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

Of course you can also use a third-party library, which is pretty good to write. He expands the system's slide-back gesture without sliding from the edge and returns as long as the right slide is on. Most importantly, he only needs to be in the project, without a single line of code. Attach GitHub URL

6. Add head view to WebView

WebView is a composite view that contains a scrollview,scrollview inside is a uiwebbrowserview (responsible for displaying the contents of WebView)

UIView *webbrowserview =Self. WebView. ScrollView. subviews[0];Get WebView's Webbrowserview.Self. Backheadimageview = [[Uiimageview Alloc]initwithframe:CGRectMake (0,0, Kscreenwidth, kscreenwidth*2/3.0)]; [_backheadimageview sd_setimagewithurl:[Nsurl URLWithString:Self. ImageUrl] Placeholderimage:[UIImage imagenamed:@ "Placeholderimage"]; [self.webview Insertsubview:_backheadimageview Belowsubview:self.webview//insert Backheadimageview under WebView scrollview self.webbrowserview.frame; Frame.origin.y =  Cgrectgetmaxy (_backheadimageview.frame); self.webbrowserview.frame = Frame //change Webbrowserview frame to move down backheadimageview height to make it visible       
7. Animation settings for modal jumps

Animate the modal jumps with four options available

    DetailViewController *detailVC = [[DetailViewController alloc]init];    //UIModalTransitionStyleFlipHorizontal 翻转    //UIModalTransitionStyleCoverVertical 底部滑出    //UIModalTransitionStyleCrossDissolve 渐显    //UIModalTransitionStylePartialCurl 翻页    detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl; [self presentViewController:detailVC animated:YES completion:nil];
8, picture processing only to get part of the picture
uiimage *image = [UIImage Imagenamed:filename]; cgimageref imageref = Imagecgrect rect = cgrectmake (Origin .x, Origin.y, Size.width, Size.height); //here the width is relative to the real size of the picture //such as your picture is 400x400 so (0,0,400, 400) is the full size of the picture, want to take which part of the corresponding coordinates can be set cgimageref imagerefrect =  Cgimagecreatewithimageinrect (Imageref, rect); uiimage *imagerect = [[uiimage alloc] Initwithcgimage:imagerefrect";             
9. Set the picture for UIView
    UIImage *image = [UIImage imageNamed:@"playing"];    _layerView.layer.contents = (__bridge id)image.CGImage;_layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);//同样可以设置显示的图片范围//不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,widt,height
10. Add simple animations to TableView or CollectionView cells, like this
888.gif
555.gif


Just animate the cell that will be displayed in the Willdisplaycell method:

- (void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) indexpath{Nsarray *Array = tableview.indexpathsforvisiblerows; Nsindexpath *firstindexpath =array[0];Set Anchorpoint Cell.layer.anchorPoint = Cgpointmake (0,0.5);To prevent cell view from moving, re-put the cell back in its original position cell.layer.position = Cgpointmake (0, CELL.LAYER.POSITION.Y);Set the cell to rotate 90 degrees according to the z axis, note that it is radiansif (Firstindexpath.row < Indexpath.row) {cell.layer.transform = Catransform3dmakerotation (m_pi_2,0,0,1.0); }else{Cell.layer.transform = catransform3dmakerotation (-m_pi_2,0,0,1.0); } Cell.alpha =0.0; [UIView animatewithduration:1 animations:^{cell.layer.transform = catransform3didentity; Cell.alpha = 1.0;}];} -(void) CollectionView: (Uicollectionview *) CollectionView Willdisplaycell: ( Uicollectionviewcell *) cell Foritematindexpath: (Nsindexpath *) indexpath{if ( Indexpath.row% 2! = 0) {cell.transform = Cgaffinetransformtranslate (Cell.transform, Kscreenwidth/2, 0 ); }else{cell.transform = Cgaffinetransformtranslate (Cell.transform,-kScreenWidth/2, 0); } Cell.alpha = 0.0; [UIView animatewithduration:0.7 animations:^{cell.transform = cgaffinetransformidentity; Cell.alpha = 1.0;} completion:^ (BOOL finished) {}];}      

 

IOS10 A practical trick (there's always something you don't know and you'll use)

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.