你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS雜記(二)分享給大家,總有一條是你想要的!

iOS雜記(二)分享給大家,總有一條是你想要的!

編輯:IOS開發綜合

一.iphone程序中實現截屏的方法


在iphone程序中實現截屏的一種方法:
//導入頭文件
#import QuartzCore/QuartzCore.h
//將整個self.view大小的圖層形式創建一張圖片image UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//然後將該圖片保存到圖片圖

UIImageWriteToSavedPhotosAlbum(image,self,nil,nil);

截取屏幕圖片
//創建一個基於位圖的圖形上下文並指定大小為CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));

//renderInContext 呈現接受者及其子范圍到指定的上下文
[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];

//返回一個基於當前圖形上下文的圖片
UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();

//移除棧頂的基於當前位圖的圖形上下文
UIGraphicsEndImageContext();

//以png格式返回指定圖片的數據
imageData = UIImagePNGRepresentation(aImage);

二.Objective-c 畫圖

1.顏色和字體

UIKit提供了UIColor和UIFont類來進行設置顏色和字體,

UIColor *redColor=【UIColor redColor】;

【redColor set】;//設置為紅色

UIFont *front=【UIFont systemFontOfSize:14.0】;//獲得系統字體

【myLable setFont:font】;//設置文本對象的字體

2.drawRect方法
對於畫圖,你首先需要重載drawRect方法,然後調用setNeedsDisplay方法讓系統畫圖:
-(void)drawRect:(CGRect)rect;//在rect指定的區域畫圖

-(void)setNeedsDisplay;//讓系統調用drawRect畫圖

3.CoreGraphics API

UiKit所提供的畫圖類比較簡單,就是我們上面所說的UIRectFill和UIRectFrame兩個方法。對於復雜的畫圖。你需要 使用CoreGraphics API.

步驟一:獲得當前畫圖的上下文(CGContextRef) UIGraphicsGetCurrentContext(void);

步驟二:定義一個圖的軌跡(path),比如你要畫一個三角形,那麼,第一步就是畫出這個三角形的輪廓。但是並不在屏幕上顯示該圖。

步驟三:設置填充顏色

步驟四: 設置圖框顏色

步驟五:讓系統畫圖,這是你就看到了所化的圖形


三.關於控制器Controller的思考
iPhone開發中,只有一個窗口,對應的是多個視圖,而視圖的組織形式各種各樣,關鍵是要靠控制器來組織各個視圖的邏輯關系。大體的關系如下:


窗體---主控制器(比如說導航控制器),主控制器在窗體裡面,拖動過去即可,在AppDelegate中寫相關變量的代碼---在主控制器下有別的控制器,比如視圖控制器,可以通過interfacebuilder來關聯根視圖什麼的----視圖控制器相當於一個根視圖,可以調用其他的視圖---視圖中包含類文件(.h,.m)和圖形界面文件(.xib)(兩個之間必須關聯起來。)

四.翻頁效果
經常看到iPhone的軟件向上向下翻頁面的效果,其實這個很簡單,已經有封裝好的相關方法處理。
//首先設置動畫的相關參數
[UIView beginAnimations:@"Curl"context:nil];
[UIView setAnimationDuration:1.25]; //時間
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];//速度
//然後設置動畫的動作和目標視圖
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
參數UIViewAnimationTransitionCurlUp代表向上翻頁,如果向下的話UIViewAnimationTransitionCurlDown.
forView那把當前的視圖傳進去。
//最後提交動畫
[UIView commitAnimations];
五.讓一個UIImageView響應點擊事件
UIImageView *imgView =[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,44)];
imgView.userInteractionEnabled=YES;
UITapGestureRecognizer *singleTap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(onClickImage)];
[imgView addGestureRecognizer:singleTap];
[singleTap release];
-(void)onClickImage{
// here, do whatever you wantto do
NSLog(@"imageview is clicked!");
}

六.啟動界面的制作
iPhone開發實現splash畫面非常簡單,做一個全屏的歡迎頁的圖片,把它命名為Default.png,然後放在Xcode工程的Resource裡面。
在XXXAppDelegate.m程序中,插入如下代碼:
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//–inserta delay of 5 seconds before the splash screendisappears–
[NSThread sleepForTimeInterval:5.0];
//Override point for customization after applicationlaunch.
//Add the view controller’s view to the window anddisplay.
[windowaddSubview:viewController.view];
[windowmakeKeyAndVisible];
return YES;
}
這樣splash頁面就停留5秒後,消失了。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved