当前位置:编程学习 > wap >>

IOS学习笔记(7)UIButton UIImageView UIScrollView UIWebView

使用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>
@inte易做图ce 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 *iPhoneI
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,