您的位置:首页 > 其它

iPhone第五节:多媒体和手势

2015-09-07 10:12 399 查看
多媒体和手势

#import <UIKit/UIKit.h>

@interface CameraViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>

@property (nonatomic, strong) UIImageView * cameraImageView;

@end

#import "CameraViewController.h"
#import "MovieViewController.h"
#import "GestureViewController.h"
#import "WorkViewController.h"

@interface CameraViewController ()

@end

@implementation CameraViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
//背景
self.view.backgroundColor = [UIColor whiteColor];

UIImage * image = [UIImage imageNamed:@"壁纸1.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//初始化
[self initCameraBtnAndCameraImageView];

#pragma mark ==Button
UIButton * btnLogin1 = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogin1.frame = CGRectMake(45, 415, 230, 45);
btnLogin1.showsTouchWhenHighlighted = YES;
[btnLogin1 setTitle:@"音视频播放" forState:UIControlStateNormal];
[btnLogin1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLogin1 addTarget:self action:@selector(movie) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLogin1];

#pragma mark ==Button
UIButton * btnLogin2 = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogin2.frame = CGRectMake(45, 460, 230, 45);
btnLogin2.showsTouchWhenHighlighted = YES;
[btnLogin2 setTitle:@"手势演示" forState:UIControlStateNormal];
[btnLogin2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLogin2 addTarget:self action:@selector(gesture) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLogin2];

#pragma mark ==Button
UIButton * btnLogin3 = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogin3.frame = CGRectMake(45, 505, 230, 45);
btnLogin3.showsTouchWhenHighlighted = YES;
[btnLogin3 setTitle:@"Work演示" forState:UIControlStateNormal];
[btnLogin3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLogin3 addTarget:self action:@selector(work) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLogin3];
}

//初始化
- (void)initCameraBtnAndCameraImageView
{
//新建btn,添加事件
UIButton * cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cameraBtn.frame = CGRectMake(120, 150, 80, 120);
[cameraBtn addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cameraBtn];

//添加图片视图
_cameraImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 150, 80, 120)];
_cameraImageView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_cameraImageView];
}

- (void)showActionSheet
{
//下方推出菜单
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];
[actionSheet showInView:self.view];
}

//设置点击菜单的响应
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[self addCamera];
}
else if (buttonIndex == 1)
{
[self addPhoto];
}
}

//打开相机
- (void)addCamera
{
//判断是否可以打开相机,模拟器此功能无法使用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//创建一个调出拍照的控制器
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//可以选取某一部分
picker.allowsEditing = YES;
//资源类型,摄像头(相册、保存)
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法启用相机" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
}

- (void)addPhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法启用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
}

//选择结束调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//info传的字典,根据键得到值,给image赋值(编辑过的,源图)
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
//图片存入相册
//UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
_cameraImageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}

//点击取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)movie
{
MovieViewController * movieVC = [[MovieViewController alloc] init];
movieVC.modalTransitionStyle = 1;
[self presentViewController:movieVC animated:YES completion:nil];
}

- (void)gesture
{
GestureViewController * gestureVC = [[GestureViewController alloc] init];
gestureVC.modalTransitionStyle = 1;
[self presentViewController:gestureVC animated:YES completion:nil];
}

- (void)work
{
WorkViewController * workVC = [[WorkViewController alloc] init];
workVC.modalTransitionStyle = 1;
[self presentViewController:workVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end

#import <UIKit/UIKit.h>

@interface MovieViewController : UIViewController

@end

#import "MovieViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface MovieViewController ()

@property(nonatomic, strong) AVAudioPlayer *player;

@end

@implementation MovieViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

#pragma mark ==Button
UIButton * btnLogin1 = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogin1.frame = CGRectMake(45, 100, 230, 45);
btnLogin1.showsTouchWhenHighlighted = YES;
[btnLogin1 setTitle:@"播放视频" forState:UIControlStateNormal];
[btnLogin1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLogin1 addTarget:self action:@selector(moviePlay) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLogin1];

#pragma mark ==Button
UIButton * btnLogin2 = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogin2.frame = CGRectMake(45, 200, 230, 45);
btnLogin2.showsTouchWhenHighlighted = YES;
[btnLogin2 setTitle:@"播放音频" forState:UIControlStateNormal];
[btnLogin2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLogin2 addTarget:self action:@selector(voicePlay) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLogin2];
}

- (void)moviePlay
{
//本地视频
// NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"];
// NSURL * url = [[NSURL alloc] initFileURLWithPath:path];
// MPMoviePlayerViewController * player =
4000
[[MPMoviePlayerViewController alloc] initWithContentURL:url];
// //弹出视图
// //[self presentMoviePlayerViewControllerAnimated:player];
// //[self presentViewController:player animated:YES completion:nil]; //也可以
//
// //子视图
// [player.view setFrame:CGRectMake(0, 100, 320, 400)];
// [self.view addSubview:player.view];
// //[player play]; //MPMoviePlayerController 才可以用play

//网络视频
// NSURL * url = [NSURL URLWithString:@"http://123.57.75.80:8001/data//news/video//2015/06/20150609134637_60491.mp4"];
// if (url)
// {
// MPMoviePlayerViewController * player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
// //资源类型
//// MPMovieSourceTypeUnknown,
//// MPMovieSourceTypeFile, // Local or progressively downloaded network content
//// MPMovieSourceTypeStreaming // Live or on-demand streaming content
// player.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
// //控制风格
//// MPMovieControlStyleNone, // No controls
//// MPMovieControlStyleEmbedded, // Controls for an embedded view,默认,嵌入
//// MPMovieControlStyleFullscreen, // Controls for fullscreen playback
// player.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
// //设置全屏
// player.moviePlayer.fullscreen = YES;
// //缩放模式
//// MPMovieScalingModeNone, // No scaling
//// MPMovieScalingModeAspectFit, // Uniform scale until one dimension fits
//// MPMovieScalingModeAspectFill, // Uniform scale until the movie fills the visible bounds. One dimension may have clipped contents
//// MPMovieScalingModeFill // Non-uniform scale. Both render dimensions will exactly match the visible bounds
// player.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
//// UIModalTransitionStyleCoverVertical = 0,
//// UIModalTransitionStyleFlipHorizontal,
//// UIModalTransitionStyleCrossDissolve,
////#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
//// UIModalTransitionStylePartialCurl,
////#endif
// player.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
// [self presentMoviePlayerViewControllerAnimated:player];
// }
// if (!url)
// {
// NSLog(@"ERROR");
// }

//网络视频
NSURL *url = [NSURL URLWithString:@"http://123.57.75.80:8001/data//news/video//2015/06/20150609134637_60491.mp4"];
MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
playVC.view.frame = CGRectMake(0, 100, 320, 200);
[playVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
playVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
[self.view addSubview:playVC.view];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFull) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
//[self presentMoviePlayerViewControllerAnimated:playVC];
}

- (void)voicePlay
{
// //本地音频
// NSString * path = [[NSBundle mainBundle] pathForResource:@"一江水" ofType:@"mp3"];
// if (path)
// {
// NSURL * sUrl = [[NSURL alloc] initFileURLWithPath:path];
// AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:sUrl error:nil];
// [player prepareToPlay];
// player.numberOfLoops = 0;
// player.volume = 1.0f;
// if (player)
// {
// if (![player isPlaying])
// {
// [player play];
// }
// }
// }

//网络音频
NSURL *url = [NSURL URLWithString:@""];

MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
playVC.view.frame = CGRectMake(0, 100, 320, 200);
[playVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
playVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
[self.view addSubview:playVC.view];

}

-(void)changeFull
{
NSLog(@"++++++++++");
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

#import <UIKit/UIKit.h>

@interface GestureViewController : UIViewController

@property (nonatomic, retain) IBOutlet UIImageView * imageView;

@end

#import "GestureViewController.h"

@interface GestureViewController ()

@end

@implementation GestureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
//点击一次
UITapGestureRecognizer * tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addOne)];
[tapOnce setNumberOfTapsRequired:1]; //点击次数
tapOnce.numberOfTouchesRequired = 1; //手指数量
[self.view addGestureRecognizer:tapOnce];

//点击两次
UITapGestureRecognizer * tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addTwo)];
[tapTwice setNumberOfTapsRequired:2];
tapTwice.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tapTwice];
//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
[tapOnce requireGestureRecognizerToFail:tapTwice];

//长按
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addThree:)];
[self.view addGestureRecognizer:longPress];

//长按(无效)
UILongPressGestureRecognizer * longPressImage = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addSeven)];
_imageView.userInteractionEnabled = YES;
[_imageView addGestureRecognizer:longPressImage];

//上滑
UISwipeGestureRecognizer * swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(addFour)];
//滑动方向
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];

//下滑
UISwipeGestureRecognizer * swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(addFive)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDown];

//旋转
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(addSix)];
[self.view addGestureRecognizer:rotation];

//捏合
UIPinchGestureRecognizer * pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(addSeven)];
[self.view addGestureRecognizer:pin];
}

- (void)addOne
{
self.imageView.image = [UIImage imageNamed:@"1.jpg"];
}

- (void)addTwo
{
self.imageView.image = [UIImage imageNamed:@"2.jpg"];
}

- (void)addThree:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
_imageView.image = [UIImage imageNamed:@"3.jpg"];
}

- (void)addFour
{
_imageView.image = [UIImage imageNamed:@"4.jpg"];
}

- (void)addFive
{
_imageView.image = [UIImage imageNamed:@"5.jpg"];
}

- (void)addSix
{
_imageView.image = [UIImage imageNamed:@"6.jpg"];
}

- (void)addSeven
{
_imageView.image = [UIImage imageNamed:@"7.jpg"];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iPhone课程