iOS项目--古典音乐浏览
#import "ShowViewController.h"
#import "MusicCollectionViewCell.h"
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@interface ShowViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)AVAudioPlayer *player;
@end
@implementation ShowViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle]URLForResource:@"不灭" withExtension:@"mp3"];
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
[self.player play];
CGFloat imageWidth = self.scrollView.frame.size.width;
CGFloat imageHeight = self.scrollView.frame.size.height;
for (int i = 0 ; i<15; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i+1]];
UIImageView *imgView = [[UIImageView alloc]initWithImage:image];
CGFloat imageX = i * imageWidth;
CGFloat imageY = 0;
imgView.frame =CGRectMake(imageX, imageY, imageWidth, imageHeight);
[self.scrollView addSubview:imgView];
}
self.pageCtr.numberOfPages = 15;
self.scrollView.contentSize = CGSizeMake(15 * imageWidth, 0);
self.scrollView.delegate = self;
[self addTimer];
// Do any additional setup after loading the view.
}
-(void)addTimer{
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
- (IBAction)stopClick {
int flag = 0;
flag+=1;
if (flag %2 != 0) {
[self.player stop];
}else if (flag %2 == 0){
[self.player play];
}
}
-(void)nextPage
{
int page = (int)self.pageCtr.currentPage;
if (page == 14) {
page = 0;
}else{
page++;
}
self.scrollView.contentOffset = CGPointMake(page * self.scrollView.frame.size.width, 0);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = (self.scrollView.frame.size.width + self.scrollView.contentOffset.x)/self.scrollView.frame.size.width;
self.pageCtr.currentPage = page;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self addTimer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}