这是在cocoaChina上看到的一个效果,自己拿来吸取一下,还记着第一次实用keep app的时候 那时候接触播放器比较少,超级好奇怎么实现的这个动画效果,无线猜测//是帧动画吗?那应该素材很多阿,内存怎么考虑?......操碎了心 无意间在cocoaChina闲逛 发现了这个效果,好激动 下载下来分析了一下 好简单- -, 请看代码加了自己的备注
#import "ViewController.h"
#import
#import
@interface ViewController ()
@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;//视频播放器
@property(nonatomic ,strong)NSTimer *timer;//定时器
@property (weak,nonatomic) IBOutletUIView *alpaView;//管理所有控件的view
@property (weak,nonatomic) IBOutletUIButton *regiset;//注册按钮
@property (weak,nonatomic) IBOutletUIButton *login; //登录按钮
@property (weak,nonatomic) IBOutletUIScrollView *scrollView;//滚动试图
@property (weak,nonatomic) IBOutletUIPageControl *pageControl;//分页指示器
@property(nonatomic ,strong)AVAudioSession *avaudioSession;//全局的音视频Session
@property (weak,nonatomic) IBOutletNSLayoutConstraint *viewWidth;
@property (weak,nonatomic) IBOutletNSLayoutConstraint *secondViewLeading;
@property (weak,nonatomic) IBOutletNSLayoutConstraint *thirdViewLeading;
@property (weak,nonatomic) IBOutletNSLayoutConstraint *fourViewLeading;
@property (weak,nonatomic) IBOutletNSLayoutConstraint *firstLabelWidth;
@property (weak,nonatomic) IBOutletNSLayoutConstraint *secondLabelWidth;
@property (weak,nonatomic) IBOutletNSLayoutConstraint *thridLabelWidth;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
/**
* 设置其他音乐软件播放的音乐不被打断
*/
self.avaudioSession = [AVAudioSessionsharedInstance];
NSError *error =nil;
[self.avaudioSessionsetCategory:AVAudioSessionCategoryAmbienterror:&error];
NSString *urlStr = [[NSBundlemainBundle]pathForResource:@"1.mp4"ofType:nil];
NSURL *url = [NSURLfileURLWithPath:urlStr];
_moviePlayer = [[MPMoviePlayerControlleralloc]initWithContentURL:url];
// _moviePlayer.controlStyle = MPMovieControlStyleDefault;
[_moviePlayerplay];
[_moviePlayer.viewsetFrame:self.view.bounds];
[self.viewaddSubview:_moviePlayer.view];
_moviePlayer.shouldAutoplay = YES; //是否自动播放
[_moviePlayer setControlStyle:MPMovieControlStyleNone];//设置不需要进度条
[_moviePlayer setFullscreen:YES]; //是否填充屏幕
[_moviePlayer setRepeatMode:MPMovieRepeatModeOne]; //是否重复播放
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(playbackStateChanged)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:_moviePlayer];
_alpaView.backgroundColor = [UIColorclearColor];
[_moviePlayer.viewaddSubview:_alpaView];
self.regiset.layer.cornerRadius = 3.0f;
self.regiset.alpha =0.4f;
self.login.layer.cornerRadius = 3.0f;
self.login.alpha =0.4f;
self.scrollView.bounces =NO;
self.scrollView.pagingEnabled = YES;
self.pageControl.currentPage = 0;
[self.pageControladdTarget:selfaction:@selector(pageChanged:)forControlEvents:UIControlEventValueChanged];
[selfsetupTimer];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)playbackStateChanged{
//取得目前状态
MPMoviePlaybackState playbackState = [_moviePlayerplaybackState];
//状态类型
switch (playbackState) {
caseMPMoviePlaybackStateStopped:
[_moviePlayerplay];
break;
caseMPMoviePlaybackStatePlaying:
NSLog(@"播放中");
break;
caseMPMoviePlaybackStatePaused:
[_moviePlayerplay];
break;
caseMPMoviePlaybackStateInterrupted:
NSLog(@"播放被中断");
break;
caseMPMoviePlaybackStateSeekingForward:
NSLog(@"往前快转");
break;
caseMPMoviePlaybackStateSeekingBackward:
NSLog(@"往后快转");
break;
default:
NSLog(@"无法辨识的状态");
break;
}
}
-(void)updateViewConstraints{
[superupdateViewConstraints];
self.viewWidth.constant = CGRectGetWidth([UIScreenmainScreen].bounds) *4 ;
self.secondViewLeading.constant = CGRectGetWidth([UIScreenmainScreen].bounds);
self.thirdViewLeading.constant = CGRectGetWidth([UIScreenmainScreen].bounds) *2;
self.fourViewLeading.constant =CGRectGetWidth([UIScreenmainScreen].bounds) *3;
self.firstLabelWidth.constant = CGRectGetWidth([UIScreenmainScreen].bounds);
self.secondLabelWidth.constant =CGRectGetWidth([UIScreenmainScreen].bounds);
self.thridLabelWidth.constant = CGRectGetWidth([UIScreenmainScreen].bounds);
}
//ios以后隐藏状态栏
-(BOOL)prefersStatusBarHidden{
returnYES;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
returnUIStatusBarStyleLightContent;
}
-(void)setupTimer{
self.timer = [NSTimertimerWithTimeInterval:3.0ftarget:selfselector:@selector(timerChanged)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop]addTimer:self.timerforMode:NSRunLoopCommonModes];
}
-(void)pageChanged:(UIPageControl *)pageControl{
CGFloat x = (pageControl.currentPage) * [UIScreenmainScreen].bounds.size.width;
[self.scrollViewsetContentOffset:CGPointMake(x,0) animated:YES];
}
-(void)timerChanged{
int page = (self.pageControl.currentPage +1) %4;
self.pageControl.currentPage = page;
[selfpageChanged:self.pageControl];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
double page =self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
self.pageControl.currentPage = page;
if (page== -1)
{
self.pageControl.currentPage =3;//序号0最后1页
}
else if (page == 4)
{
self.pageControl.currentPage =0; //最后+1,循环第1页
[self.scrollViewsetContentOffset:CGPointMake(0,0) animated:YES];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.timerinvalidate];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[selfsetupTimer];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end