iOS 8案例开发大全
上QQ阅读APP看书,第一时间看更新

实例018 伸缩屏幕中的视图

实例说明

本实例的功能是,使用UIView的属性contentStretch可以设置并改变视图的内容如何拉伸。首先在屏幕上方设置了UIImageView区域,在此区域显示一幅图片。在屏幕中间用x坐标设置了视图的宽度,用y坐标设置了视图的高度。在下方设置了“origin”和“size”两个按钮。

具体实现

实例文件UIkitPrjContentStretch.h的实现代码如下所示。

#import "SampleBaseController.h"
@interface UIKitPrjContentStretch : SampleBaseController
{
 @private
  UIImageView* imageView_;
  CGRect rect_;
  UILabel* label_;
}
@end

实例文件UIkitPrjContentStretch.m的实现代码如下所示。

#import "UIKitPrjContentStretch.h"
#pragma mark ----- Private Methods Definition -----
@interface UIKitPrjContentStretch ()
- (void)originDidPush;
- (void)sizeDidPush;
- (void)changeLabelCaption;
@end
#pragma mark ----- Start Implementation For Methods -----
@implementation UIKitPrjContentStretch
// finalize
- (void)dealloc {
  [imageView_ release];
  [label_ release];
  [super dealloc];
}
- (void)viewDidLoad {
  [super viewDidLoad];
  rect_ = CGRectMake( 0.0, 0.0, 1.0, 1.0 );
  // 背景设置成黑色
  self.view.backgroundColor = [UIColor blackColor];
  // 追加图像View
  NSString* path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],
@"dog.jpg"];
  UIImage* image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
  imageView_ = [[UIImageView alloc] initWithImage:image];
  imageView_.frame = CGRectMake( 0, 0, 320, 320 );
  imageView_.contentMode = UIViewContentModeScaleAspectFit;
  imageView_.contentStretch = rect_;
  [self.view addSubview:imageView_];
  // 追加标签
  label_ = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 320, 60 )];
  CGPoint newPoint = imageView_.center;
  newPoint.y += 190;
label_.center = newPoint;
label_.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label_];
[self changeLabelCaption];
// 追加origin按钮
UIButton* originChange = [UIButton buttonWithType:UIButtonTypeRoundedRect];
originChange.frame = CGRectMake( 0, 0, 100, 40 );
newPoint = self.view.center;
newPoint.x -= 50;
newPoint.y = self.view.frame.size.height -40;
originChange.center = newPoint;
[originChange setTitle:@"origin" forState:UIControlStateNormal];
[originChange addTarget:self
            action:@selector(originDidPush)
    forControlEvents:UIControlEventTouchUpInside];
// 追加size修改按钮
UIButton* sizeChange = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sizeChange.frame = CGRectMake( 0, 0, 100, 40 );
newPoint = originChange.center;
newPoint.x += 100;
sizeChange.center = newPoint;
[sizeChange setTitle:@"size" forState:UIControlStateNormal];
[sizeChange addTarget:self
            action:@selector(sizeDidPush)
    forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:originChange];
[self.view addSubview:sizeChange];
}
#pragma mark ----- Private Methods -----
- (void)originDidPush {
if ( 0.99 < rect_.origin.x ) {
  rect_.origin.x = 0.0;
  rect_.origin.y = 0.0;
} else {
  rect_.origin.x += 0.1;
  rect_.origin.y += 0.1;
}
imageView_.contentStretch = rect_;
[self changeLabelCaption];
}
- (void)sizeDidPush {
if ( 0.09 > rect_.size.width ) {
  rect_.size.width = 1.0;
  rect_.size.height = 1.0;
} else {
  rect_.size.width -= 0.1;
  rect_.size.height -= 0.1;
}
imageView_.contentStretch = rect_;
[self changeLabelCaption];
}
- (void)changeLabelCaption {
label_.numberOfLines = 2;
label_.text = [NSString stringWithFormat:@"x = %f, y = %f, \r\nw = %f, h = %f",
                      rect_.origin.x,  rect_.origin.y,  rect_.size.width,  rect_.size.
height];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
@end

执行后的效果如图2-22所示。

图2-22 执行效果