IOS學習筆記(7)UIButton UIImageView UIScrollView UIWebView – iPhone手機開發技術文章 iPhone軟體開發教學課程

使用UIButton給用戶界面添加按鈕

@property(nonatomic,strong)UIButton *myButton;

@synthesize myButton;

-(void)buttonIsPressed:(UIButton *)paramSender{

    NSLog(@"Button is pressed.");

}

-(void)buttonIsTapped:(UIButton *)paramSender{

    NSLog(@"Button is tapped.");

}

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.myButton = [ UIButton buttonWithType:UIButtonTypeRoundRect];

    self.myButton.frame = CGRectMake(110.0f,200.0f,100.0f,37.0f);

    [self.myButton setTitle:@"Press Me" forState:UIControlStateNormal];

    [self.myButton setTitle:@"I'm Pressed" forState:UIControlStateHighlighted];

    [self.myButton addTarget:self action:@selector(buttonIsPressed:) forControlEvents:UIControlEventTouchDown];

    [self.myButton addTarget:self action:@selector(buttonIsTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.myButton];

}

typedef enum{

    UIButtonTypeCustom = 0,

    UIButtonTypeRoundedRect,

    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

}UIButtonType;

按鈕上增加圖片:

UIImage *normalImage = [UIImage imageNamed:@"NormalBlueButton.png"];

UIImage *highlightedImage = [UIImage imageNamed:@"highlightedBlueButton.png"];

self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];

self.myButton.frame = CGRectMake(110.0f,200.0f,100.0f,37.0f);

[self.myButton setBackgroundImage:normalImage forState:UIControlStateNormal];

[self.myButton setTitle:@"Normal" forState:UIControlStateNormal];

[self.myButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];

[self.myButton setTitle:@"Pressed" forState:UIControlStateHighlighted];

 

使用UIImageView顯示圖片

@property( nonatomic,strong)UIImageView *myImageView;

@synthesize myImageView;

-(void)viewDidLoad{

    [super viewDidLoad];

    UIImage *macBookAir = [UIImage imageNamed:@"MacBookAir.png"];

    self.myImageView = [[UIImageView alloc]initWithImage:macBookAir];

self.myImageView.center = self.view.center;

    [self.view addSubview:self.myImageView];

}

我在網上下載的圖片是 980*519的像素,當然不適合iPhone的屏幕瞭,如果像上面那樣加載,得到的效果肯定不是你想要的。怎麼解決這個問題是個關鍵:

通過設置視圖的contentMode屬性來改善這個問題。

typedef enum{

    UIViewContentModeScaleToFill,//圖片視圖裡的圖片將圖片視圖填滿

    UIViewContentModeScaleAspectFit,//確保圖片視圖裡的圖片有正確的長寬比,並且會確保圖片適應圖片視圖的邊界

    UIViewContentModeScaleAspectFill ,//確保長寬比,並且使圖片充滿整個視圖邊界,為瞭能使這個值正常工作,確保將 clipsToBounds這個屬性設為YES。

    UIViewContentModeRedraw,

    UIViewControlModeCenter,

    UIViewControlModeTop,

    UIViewControlModeButtom,

    UIViewControlModeLeft,

    UIViewControlModeRight,

    UIViewControlModeTopLeft,

    UIViewControlModeTopRight,

    UIViewControlModeBottomLeft,

    UIViewControlModeBottomRight,    

}UIViewContentMode;

 

使用UIScrollView創建能滑動的內容

@property( nonatomic,strong)UIImageView *myImageView;

@property( nonatomic,strong)UIScrollView *myScrollView;

@synthesize myImageView;

@synthesize myScrollView;

 

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    UIImage *imageToLoad = [UIImage imageNamed:"macBookAir.png"];

    self.myImageView = [[UIImageView alloc]initWithImage:imageToLoad];

    self.myScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];

    [self.myScrollView addSubview:self.myImageView];

    self.myScrollView.contentSize = self.myImageView.bounds.size;

    [self.view addSubview:self.myScrollView];

}

註:圖片尺寸小於滑動視圖尺寸不起作用。

UIScrollViewDelegate協議:

scrollViewDidScroll:當滑動視圖裡的內容滑動時將調用這個方法。

scrollViewWillBeginDeceleratin:當用戶滑動滑動視圖裡的內容並且手指離開屏幕而且內容還在滑動的時候,將會調用這個方法。

scrollViewDidEndDecelerating :當滑動視圖裡的內容結束滑動時將調用這個方法。

scrollViewDidEndDragging: willDecelerating: 當用戶完成拖拽將會調用這個方法。(無滑動慣性)

#import <UIKit/UIKit.h>

@interface Creating_Scrollable_Content_with_UIScrollViewViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *myImageView;

@property (nonatomic, strong) UIScrollView *myScrollView;

@end

 

– (void)scrollViewDidScroll:(UIScrollView *)scrollView{ /* Gets called when user scrolls or drags */ self.myScrollView.alpha = 0.50f;

}

 

– (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ /* Gets called only after scrolling */

self.myScrollView.alpha = 1.0f;

}

 

– (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

/* Make sure the alpha is reset even if the user is dragging */ self.myScrollView.alpha = 1.0f;

 

}

– (void)viewDidLoad{

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

UIImage *imageToLoad = [UIImage imageNamed:@"MacBookAir.png"]; self.myImageView = [[UIImageView alloc] initWithImage:imageToLoad]; self.myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; [self.myScrollView addSubview:self.myImageView]; self.myScrollView.contentSize = self.myImageView.bounds.size; self.myScrollView.delegate = self;

[self.view addSubview:self.myScrollView];

}

 

滑動指示器(可以分頁顯示內容)

self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhile;

self.myScrollView.pagingEnabled = YES;//禁用分頁

– (void)viewDidLoad{

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

UIImage *iPhone = [UIImage imageNamed:@"iPhone.png"];

UIImage *iPad = [UIImage imageNamed:@"iPad.png"];

UIImage *macBookAir = [UIImage imageNamed:@"MacBookAir.png"]; CGRect scrollViewRect = self.view.bounds;

self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect]; self.myScrollView.pagingEnabled = YES;

self.myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width * 3.0f, scrollViewRect.size.height);

[self.view addSubview:self.myScrollView];

CGRect imageViewRect = self.view.bounds;

UIImageView *iPhoneImageView = [self newImageViewWithImage:iPhone frame:imageViewRect];

[self.myScrollView addSubview:iPhoneImageView];

 

/* Go to next page by moving the x position of the next image view */ imageViewRect.origin.x += imageViewRect.size.width;

UIImageView *iPadImageView = [self newImageViewWithImage:iPad frame:imageViewRect];

 

[self.myScrollView addSubview:iPadImageView];

/* Go to next page by moving the x position of the next image view */ imageViewRect.origin.x += imageViewRect.size.width; UIImageView *macBookAirImageView =

[self newImageViewWithImage:macBookAir frame:imageViewRect];

[self.myScrollView addSubview:macBookAirImageView];

}

 

使用UIWebView加載Web頁面

loadData: MIMEType: textEncodingName: baseURL:  //加載一個NSData的實例到網頁視圖上

loadHTMLString: baseURL:  //這個方法是加載NSString的一個實例到網頁視圖上,這個實例必須是一個有效的HTML,或者說是那些網頁能提供的東西。

loadRequest://加載一個NSURLRequest的實例,當你想要在應用程序的網頁視圖裡加載遠程的URL時,這個方法是很有用的。

@property( nonatomic,strong)UIWebView *myWebView;

@synthesize myWebView;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.myWebView = [ [UIWebView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:self.myWebView];

    NSString *htmlString = @"hello world <strong>Cookbook</strong>";

    [self.myWebView loadHTMLString:htmlString baseURL:nil];

}

加載Apple的主頁面:

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.myWebView = [ [UIWebView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:self.myWebView];

    NSURL *url = [NSURL URLWithString:@"https://www.apple.com"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [self.myWebView loadRequest:request];

}

Safari加載內容時屏幕左上角會出現一個活動指示器,我們也做一個:

使用協議 :UIWebViewDelegate

#import <UIKit/UIKit.h>

@interface WebViewController:UIViewController<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *myWebView;

@end

 

/* webViewDidStartLoad:當網頁視圖開始加載內容時將調用這個方法

webViewDidFinishLoad: 當網頁視圖完成加載時將調用這個方法

webView:didFailLoadWithError: 當因加載出錯而導致停止加載時將調用這個方法。 

*/

-(void)webViewDidStartLoad:(UIWebView *)webView{

    [ [UIApplication sharedApplication]setNetworkActivityIndicatorVieible:YES];

}

-(void)webViewDidFinishLoad{

    [ [UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];

}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    [ [UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

} www.aiwalls.com

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundCOlor = [UIColor whiteColor];

    self.myWebView = [[UIWebView alloc]initWithFrame:self.view.bounds];

self.myWebView.delegate = self;

    self.myWebView.scalesPageToFit = YES;

    [self.view addSubview:myWebView];

    NSURL *url = [NSURL URLWithString:@"https://www.apple.com"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    self.myWebView loadRequest:request];

}

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *