IOS開發一些常用功能

來源:互聯網
上載者:User

1 點擊背景時關閉鍵盤
在viewDidLoad中添加手勢辨識器

// add tap gesture recognizer    UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]                                              initWithTarget:self                                              action:@selector(handleBackgroundTap:)];    tapRecognizer.cancelsTouchesInView = NO;    [self.view addGestureRecognizer:tapRecognizer];

 

定義函數

- (void) handleBackgroundTap:(UITapGestureRecognizer*)sender{    [phoneField resignFirstResponder];    [nameField resignFirstResponder];    [postcodeField resignFirstResponder];    }

在該函數使文字欄位取消第一響應者狀態.

2 圖片作為填充顏色
[UIColor colorWithPatternImage: [UIImage imageNamed:@"background"];

3 建立和繪製漸層

View Code

 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();        UIColor *startColor = [UIColor blueColor];    CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]);        UIColor *endColor = [UIColor greenColor];    CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]);        CGFloat colorComponents[8] = {        startColorComponents[0],        startColorComponents[1],        startColorComponents[2],        startColorComponents[3],        endColorComponents[0],        endColorComponents[1],        endColorComponents[2],        endColorComponents[3],    };        CGFloat colorIndices[2] = {        0.0f,        1.0f,    };    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, (const CGFloat *)&colorComponents, (const CGFloat *)&colorIndices, 2);        CGColorSpaceRelease(colorSpace);        CGRect screenBounds = [[UIScreen mainScreen] bounds];        CGPoint startPoint, endPoint;        CGContextRef context = UIGraphicsGetCurrentContext();    startPoint = CGPointMake(0.0f,screenBounds.size.height/2.0f);    endPoint = CGPointMake(screenBounds.size.width, startPoint.y);    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);    CGGradientRelease(gradient);

 4 載入顯示網頁

View Code

NSURL *url = [NSURL URLWithString:_detailItem];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    [_webView loadRequest:request];

 5 導航條載入圖片做背景


6 UIView加陰影

View Code

sepLine = [[UIView alloc] init ]; sepLine.frame = CGRectMake(LEFT_MARGIN+5, 0, cellWidth-LEFT_MARGIN-RIGHT_MARGIN, 1);    //加陰影     CAGradientLayer *bottomShadow = [[CAGradientLayer alloc] init];    bottomShadow.frame = CGRectMake(2, 0, cellWidth-LEFT_MARGIN-RIGHT_MARGIN+2, 1);    CGColorRef black = [UIColor lightGrayColor].CGColor;    CGColorRef white = [UIColor whiteColor].CGColor;    bottomShadow.colors = [NSArray arrayWithObjects:(__bridge id)black,(__bridge id)white, nil];    [sepLine.layer addSublayer:bottomShadow];

 7 iOS 同步 synchronization
使用NSLock和@synchronized來保護臨界區
還可以用dispatch_sync

8 擷取螢幕資訊
UIScreen用於擷取當前所有螢幕(包括裝置主畫面)的螢幕對象。螢幕對象包含螢幕的屬性資訊(包括將螢幕的尺寸和
像素比例也考慮在內的螢幕特徵)。

 9 資產庫架構
iOS 4.0引入了資產庫架構(AssetsLibrary.framework),該架構提供一個查詢介面,您可以通過它尋找使用者照片和資料。通過使用該架構,您可以訪問Photos管理的資產,包括使用者儲存的相簿以及匯入到裝置中的圖片或視頻。而且您也可以將照片或者視頻儲存到使用者的相簿。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.