//實例化一個UIImageView的對象 _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 30, 355, 300)]; //設置一個比較凸顯的特性(背景顏色或者圖片) //frame和bounds的屬性區別是frame所有都變化,bounds只改變大小 _imageView.frame = CGRectMake(10, 0, 355, 200); _imageView.bounds = CGRectMake(100, 50, 355, 200); _imageView.backgroundColor = [UIColor purpleColor]; //UIimageView是顯示圖片的控件,設置圖片屬性 _imageView.image = [UIImage imageNamed:@"01.png"]; //加載到根視圖(self.view) [self.view addSubview:_imageView];數組
UIButton *start = [[UIButton alloc]initWithFrame:CGRectMake(10, 340, 100, 30)]; [start setTitle:@"開始" forState:UIControlStateNormal]; [start setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [start addTarget:self action:@selector(beginStart:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:start]; UIButton *move = [[UIButton alloc]initWithFrame:CGRectMake(130, 340, 100, 30)]; [move setTitle:@"移動" forState:UIControlStateNormal]; [move setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [move addTarget: self action:@selector(moveView:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:move]; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30, 0, 200, 100)]; view.backgroundColor = [UIColor redColor];
// [self.view addSubview:view];ide
//給imageView添加動畫效果 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:15]; [UIView setAnimationRepeatCount:3]; _imageView.frame = CGRectMake(30, 100, 300, 200); [UIView commitAnimations]; // [UIView animateWithDuration:5 animations:^{ //添加須要動畫效果的frame最終位置 }];
}動畫
( void )moveView:( UIButton * )sender{ //移動 //注意transform獲取的frame是最初始的frame,不是平移或者旋轉以後的frame // _imageView.transform = CGAffineTransformMakeTranslation(-10, 0); //旋轉 _imageView.transform = CGAffineTransformMakeRotation(60*M_PI/180); }代理
( void )beginStart:(UIButton *)sender{ NSArray *array = @[[UIImage imageNamed:@"01.png"],[UIImage imageNamed:@"02.jpg"],[UIImage imageNamed:@"03.jpg"]]; //設置圖片播放的數組 _imageView.animationImages = array;code
//動畫的播放完四張圖片須要的時長 _imageView.animationDuration = 1;orm
//圖片播放的循環次數設置是0 表示的循環播放 _imageView.animationRepeatCount = 0; //開始動畫 [_imageView startAnimating]; }對象
@end //設置滾動視圖的背景顏色 _scrollView.backgroundColor = [UIColor purpleColor]; //contentSize屬性必定要比scrollView的尺寸大 _scrollView.contentSize = CGSizeMake(1024, 845); //內容的偏移量 _scrollView.contentOffset = CGPointMake(-200, -200); //設置滾動視圖的代理對象 _scrollView.delegate = self;圖片
NSLog(@"imageView:x %.2f y:%.2f",_imageView.frame.origin.x,_imageView.frame.origin.y); //-----分頁------ //代碼實現滾動視圖 實例化滾動視圖 _pageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(87.5, 50, 200, 300)]; _pageScrollView.backgroundColor = [UIColor redColor]; //設置滾動視圖的內容尺寸 _pageScrollView.contentSize = CGSizeMake(200*4, 300); //設置是否分頁 _pageScrollView.pagingEnabled = YES; //設置代理對象 _pageScrollView.delegate = self; //將scrollView添加到根視圖上 [self.view addSubview:_pageScrollView]; //添加分頁的圖片 for (int i = 0; i<4; i++) { //實例化多個imageView UIImageView *pageImageView = [[UIImageView alloc]initWithFrame:CGRectMake(200*i, 0, 200, 300)]; //設置imageView的image屬性 pageImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%i.jpg",i+1]]; //將imageView添加到滾動視圖 [_pageScrollView addSubview:pageImageView]; }
}get