使用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:@"http://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];
}
-(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:@"http://www.apple.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.myWebView loadRequest:request];
}