UIImageView,uiimageview點擊事件
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. /*********UIImage***********/ //由名字直接讀取圖片 //優點:用時相對較短 //缺點:讀取之後會佔用記憶體 //如果圖片較小,用名字直接讀取 UIImage *imageName = [UIImage imageNamed:@"1.png"]; //UIImageView UIImageView *imageViewName = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 60, 60)]; imageViewName.image = imageName; [self.view addSubview:imageViewName]; //由圖片的路徑讀取圖片 //優點:讀取之後不會佔用記憶體 //缺點:用時相對較長 //如果圖片較大,用路徑直接讀取 //擷取圖片路徑:相對路徑 //第一個參數:檔案的名字 //第二個參數:檔案的類型 NSString *path = [[NSBundle mainBundle] pathForResource:@"map" ofType:@"png"]; UIImage *imagePath = [UIImage imageWithContentsOfFile:path]; //UIImageView UIImageView *imageViewPath = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, 200, 50)]; imageViewPath.image = imagePath; [self.view addSubview:imageViewPath]; /**********UIImageView-動畫效果*************/ UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 240, 60, 60)]; imageView.backgroundColor = [UIColor grayColor]; imageView.image = [UIImage imageNamed:@"1.png"]; /****動畫效果相關屬性****/ //建立圖片數組 //開闢記憶體空間 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0]; for (int i = 1; i <= 12; i ++) { [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"player%d.png",i]]]; } //設定動畫圖片,需要接收一個數組,數組裡面的類型必須是UIImage類型 imageView.animationImages = array; //動畫時間:數組裡面所有的圖片轉一圈所用時間 imageView.animationDuration = 1; //迴圈次數:大於0的數:寫幾就迴圈幾次,結束 0:無限迴圈 imageView.animationRepeatCount = 0; //tag imageView.tag = 1000; [self.view addSubview:imageView]; //開始動畫 [imageView startAnimating]; //UIButton UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(30, 330, 260, 30); button.backgroundColor = [UIColor redColor]; [button setTitle:@"button" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];}#pragma mark - 按鈕的點擊事件:停止或開始動畫- (void)buttonClick:(UIButton *)button{ //找到UIImageView UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1000]; //停止動畫// [imageView stopAnimating]; static BOOL isAnimation = YES; if (isAnimation) { [imageView stopAnimating]; isAnimation = NO; }else{ [imageView startAnimating]; isAnimation = YES; }}
//*********************************************
//時間 //第一個參數執行動作相隔的時間 //第二個參數通知給誰:self //第三個參數:執行的相關方法 //第四個參數:nil //第五個參數:是否重複執行 YES:重複 NO:不重複// [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer:) userInfo:nil repeats:YES]; //self.view.bounds 以(0, 0)點為起點,全屏大小的view UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.image = [UIImage imageNamed:@"back2.jpg"]; [self.view addSubview:imageView]; //動畫效果的UIImageView UIImageView *birdView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 30, 60.5, 48)]; //tag birdView.tag = 1000; //圖片數組 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0]; for (int i = 1; i <= 18; i ++) { [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"DOVE%d.png",i]]]; } /**UIImageView的動畫屬性***/ birdView.animationImages = array; birdView.animationDuration = 1; birdView.animationRepeatCount = 0; //開始動畫 [birdView startAnimating]; [imageView addSubview:birdView]; //控制bird位置 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeFrame:) userInfo:nil repeats:YES];}#pragma mark - 改變bird的位置- (void)changeFrame:(NSTimer *)timer{ UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1000]; static int i = 0; [UIView animateWithDuration:1 animations:^{ imageView.frame = CGRectMake(5 + (i++ * 10), 20 +arc4random()%30, 60.5, 48); }]; if (5 +(i++ * 10) > 320) { imageView.frame = CGRectMake(5, 20, 60.5, 48); i = 0; }}#pragma mark - NSTimer的事件- (void)timer:(NSTimer *)timer{ NSLog(@"timer");}#pragma mark - 按鈕的點擊事件- (void)buttonClick:(UIButton *)button{ //UIView的動畫效果 //第一個參數:動畫執行的時間 //第二個參數:執行動作// [UIView animateWithDuration:3 animations:^{// self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];// }]; //第一個參數:動畫執行的時間 //第二個參數:動畫順延強制的時間 //第三個參數:動畫執行的效果 //第四個參數:執行動作 //第五個參數:執行完要做的動作之後做的操作 [UIView animateWithDuration:3 delay:1 options:UIViewAnimationOptionOverrideInheritedOptions animations:^{ self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]; } completion:^(BOOL finished) {// self.view.backgroundColor = [UIColor orangeColor]; }];}