其实,UIImageVIew为UIImage的视图容器.
3.UIImage 与UIImageView的重要属性:
UIImage:
重要属性:
size
imageOrientation
重要方法:
+imageNamed:
+imageWithData:
drawAtPoint:
drawAtPoint:blendMode:alpha:
drawInRect:
drawInRect:blendMode:alpha:
UIImageView:
重要属性:
image
animationImages
重要方法:
initWithImage:
1.使用UIImageView来播放动画.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"ImagesTitle", @"");
// set up our UIImage with a group or array of images to animate (or in our case a slideshow)
self.imageView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"scene1.jpg"], [UIImage imageNamed:@"scene2.jpg"], [UIImage imageNamed:@"scene3.jpg"], [UIImage imageNamed:@"scene4.jpg"], [UIImage imageNamed:@"scene5.jpg"], nil ];
imageView.animationDuration = 5.0;
[self.imageView stopAnimating];
// Set the appropriate accessibility labels.
[self.imageView setIsAccessibilityElement:YES];
[self.imageView setAccessibilityLabel:self.title];
[self.slider setAccessibilityLabel:NSLocalizedString(@"DurationSlider",@"")];
}
2.使用UIScrollView来对子视图UIImageView进行操作,移动,放大及缩小.
//UIScrollViewDelegate ...
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [self.view viewWithTag:201];
}
/**/
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
NSLog(@"The scroll view's size is (%f,%f):",scrollView.frame.size.width,scrollView.frame.size.height);
NSLog(@"The scroll view's content size is (%f,%f):",scrollView.contentSize.width,scrollView.contentSize.height);
UIImageView *temp=(UIImageView*)[self.view viewWithTag:201];
NSLog(@"The scroll view's subview size is (%f,%f):",temp.frame.size.width,temp.frame.size.height);
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.weathermap = URLIMAGE(MAP_URL);
self.title = @"Weather Scroller";
// Create the scroll view and set its content size and delegate
UIScrollView *sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 284.0f)] autorelease];
sv.contentSize = self.weathermap.size;
sv.delegate = self;
// Create an image view to hold the weather map and add it to the scroll view
UIImageView *iv = [[[UIImageView alloc] initWithImage:self.weathermap] autorelease];
iv.userInteractionEnabled = YES;
iv.tag = 201;
// Calculate and set the zoom scale values
float minzoomx = sv.frame.size.width / self.weathermap.size.width;
float minzoomy = sv.frame.size.height / self.weathermap.size.height;
sv.minimumZoomScale = MIN(minzoomx, minzoomy);
sv.maximumZoomScale = 3.0f;
// Add in the subviews
[sv addSubview:iv];
[self.view addSubview:sv];
}
3.UIScrollView 的 scrollRectToVisible: 方法的使用:
- (void)viewDidLoad {
[super viewDidLoad];
scrollView1 =[ [UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480) ];
[scrollView1 setContentSize:CGSizeMake(320*3,400)];
[scrollView1 setPagingEnabled:YES];
scrollView1.showsHorizontalScrollIndicator=NO;
[scrollView1 setClipsToBounds:YES];
UIView *view1=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[view1 setBackgroundColor:[UIColor redColor]];
UIView *view2=[[UIView alloc] initWithFrame:CGRectMake(320,0,320,480)];
[view2 setBackgroundColor:[UIColor yellowColor]];
UIView *view3=[[UIView alloc] initWithFrame:CGRectMake(320*2,0,320,480)];
[view3 setBackgroundColor:[UIColor blueColor]];
[scrollView1 addSubview:view1];
[scrollView1 addSubview:view2];
[scrollView1 addSubview:view3];
//[self.view addSubview:scrollView1];
[self.view insertSubview:scrollView1 atIndex:0];
[view1 release];
[view2 release];
[view3 release];
[scrollView1 release];
//启动时钟来动态更改视图.
[NSTimer scheduledTimerWithTimeInterval:3
target: self
selector:@selector(scrollToRight)
userInfo:nil
repeats:YES];
}
-(void)scrollToRight
{
static int index=0;
if(index<2)
{
index++;
CGRect frame=scrollView1.frame;
frame.origin.x=320*index;
frame.origin.y=0;
[scrollView1 scrollRectToVisible:frame animated:YES];
}
else
{
scrollView1.contentOffset=CGPointMake(0,0);
//[scrollView1 scrollRectToVisible:CGRectMake(0, 0,320,480) animated:YES];
index-=3;
}
}