coreText 圖文混排之圖片拖動
1.圖文混排編輯,圖片可拖動,看動態圖
:
2.看程式碼:
.h檔案
//
// ViewController.h
// textView
//
// Created by yupeng xia on 16/9/19.
// Copyright © 2016年 yupeng xia. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
.m檔案
//
// ViewController.m
// textView
//
// Created by yupeng xia on 16/9/19.
// Copyright © 2016年 yupeng xia. All rights reserved.
//
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import
#import <MediaPlayer/MediaPlayer.h>
#import "UIImageViewBaseClass.h"
@interface ViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
UITextView* textView;
NSRange _range;
NSMutableDictionary* jsonData;
NSMutableArray* imageArray;
NSMutableArray *pathArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
jsonData = [NSMutableDictionary dictionaryWithCapacity:0];
imageArray = [NSMutableArray arrayWithCapacity:0];
pathArray = [NSMutableArray arrayWithCapacity:0];
NSString * str1 = @"asfasfa阿斯頓發生大發撒放大離開家撒旦法按時付款就阿里;雙方均asfasdfasfdalkjsflakj阿斯頓發生大發撒旦法asdfasdfaasfdaasa撒旦法;拉斯克獎發了奧斯卡獎罰洛杉磯的法律;看見誰發的阿斯利康就發;了資料庫等法律按實際開發;阿里就開始放到了;安家費阿里山科技發達了開始將對方拉開始交電費了卡雙方的空間啊傳送卡飛機阿里開始就放暑假了罰款就是浪費asfasfa阿斯頓發生大發撒放大離開家撒旦法按時付款就阿里;雙方均asfasdfasfdalkjsflakj阿斯頓發生大發撒旦法asdfasdfaasfdaasa撒旦法;拉斯克獎發了奧斯卡獎罰洛杉磯的法律;看見誰發的阿斯利康就發;了資料庫等法律按實際開發;阿里就開始放到了;安家費阿里山科技發達了開始將對方拉開始交電費了卡雙方的空間啊傳送卡飛機阿里開始就放暑假了罰款就是浪費asfasfa阿斯頓發生大發撒放大離開家撒旦法按時付款就阿里;雙方均asfasdfasfdalkjsflakj阿斯頓發生大發撒旦法asdfasdfaasfdaasa撒旦法;拉斯克獎發了奧斯卡獎罰洛杉磯的法律;看見誰發的阿斯利康就發;了資料庫等法律按實際開發;阿里就開始放到了;安家費阿里山科技發達了開始將對方拉開始交電費了卡雙方的空間啊傳送卡飛機阿里開始就放暑假了罰款就是浪費";
self.edgesForExtendedLayout = UIRectEdgeNone;
//初始化編輯框
textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)];
textView.backgroundColor = [UIColor cyanColor];
textView.text = str1;
textView.font = [UIFont systemFontOfSize:15];
[self.view addSubview:textView];
// //設定環繞的路徑
// UIBezierPath* path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
// UIBezierPath* path1 = [UIBezierPath bezierPathWithRect:CGRectMake(0, 200, 100, 100)];
// UIBezierPath* path2 = [UIBezierPath bezierPathWithRect:CGRectMake(0, 400, 100, 100)];
// textView.textContainer.exclusionPaths = @[path,path1,path2];
addImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
addImageButton.layer.cornerRadius = 30;
addImageButton.titleLabel.font = [UIFont systemFontOfSize:50];
[addImageButton setTitle:@"+" forState:UIControlStateNormal];
[addImageButton setTitle:@"+" forState:UIControlStateHighlighted];
[addImageButton setBackgroundColor:[UIColor orangeColor]];
[addImageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[addImageButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[addImageButton setFrame:CGRectMake(self.view.frame.size.width-60-20, self.view.frame.size.height-60-20, 60, 60)];
[addImageButton addTarget:self action:@selector(addImageSelectAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:addImageButton];
}
#pragma mark <--------------新增圖片-------------->
- (void)addImageSelectAction:(id)sender{
//定義圖片選擇器
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"這個是UIAlertController的預設樣式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *takePhotos = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//圖片選擇是相簿(圖片來源自相簿)
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//設定代理
picker.delegate=self;
//模態顯示介面
[self presentViewController:picker animated:YES completion:nil];
}else{
NSLog(@"不支援相機");
}
}];
UIAlertAction *photos = [UIAlertAction actionWithTitle:@"相簿" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//判斷系統是否允許選擇 相簿
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//圖片選擇是相簿(圖片來源自相簿)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//設定代理
picker.delegate=self;
//模態顯示介面
[self presentViewController:picker animated:YES completion:nil];
}
}];
[alertController addAction:takePhotos];//新增按鈕
[alertController addAction:photos];//新增按鈕
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark <--------------UIImagePickerControllerDelegate-------------->
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//通過key值獲取到圖片
UIImage * pickerImage =info[UIImagePickerControllerOriginalImage];
NSLog(@"image=%@ info=%@",pickerImage, info);
//判斷資料來源型別
switch (picker.sourceType) {
case UIImagePickerControllerSourceTypePhotoLibrary:{
// NSData* imgData = UIImageJPEGRepresentation(pickerImage, 1);
[self updateImageFrameImage:pickerImage imageType:0];
[self dismissViewControllerAnimated:YES completion:nil];
}
break;
case UIImagePickerControllerSourceTypeCamera:{
UIImageWriteToSavedPhotosAlbum(pickerImage, nil, nil, nil);
// NSData* imgData = UIImageJPEGRepresentation(pickerImage, 1);
[self updateImageFrameImage:pickerImage imageType:0];
[self dismissViewControllerAnimated:YES completion:nil];
}
break;
default:
break;
}
}
#pragma mark <--------------圖片拖動處理-------------->
- (void)doHandlePanAction:(UIPanGestureRecognizer *)paramSender{
CGPoint point = [paramSender translationInView:textView];
NSLog(@"X:%f;Y:%f",point.x,point.y);
paramSender.view.center = CGPointMake(paramSender.view.center.x + point.x, paramSender.view.center.y + point.y);
UIImageViewBaseClass* aImageView = [imageArray objectAtIndex:paramSender.view.tag-101];
aImageView.imageFrame = CGRectMake(paramSender.view.center.x + point.x - aImageView.frame.size.width/2, paramSender.view.center.y + point.y - aImageView.frame.size.height/2, aImageView.imageFrame.size.width, aImageView.imageFrame.size.height);
[self updateImageFrameImage:paramSender imageType:1];
[paramSender setTranslation:CGPointMake(0, 0) inView:textView];
}
//aImageType:0 新增圖片; aImageType:1 移動圖片;
- (void)updateImageFrameImage:(id)sender imageType:(NSInteger)aImageType{
switch (aImageType) {//新增圖片
case 0:{
UIImage *uploadImage = sender;
UIImageViewBaseClass* aImageView = [[UIImageViewBaseClass alloc] initWithFrame:CGRectMake(0, 0, 100.0f, uploadImage.size.height*(100.0f/uploadImage.size.width))];
aImageView.userInteractionEnabled = YES;
aImageView.image = uploadImage;
aImageView.imageFrame = aImageView.frame;
[textView addSubview:aImageView];
//儲存圖片
[imageArray addObject:aImageView];
//新增拖動手勢
UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(doHandlePanAction:)];
[aImageView addGestureRecognizer:panGestureRecognizer];
panGestureRecognizer.view.tag = [imageArray indexOfObject:aImageView] + 101;
//儲存圖片path
UIBezierPath * path = [UIBezierPath bezierPathWithRect:aImageView.imageFrame];
[pathArray addObject:path];
}
break;
case 1:{//移動圖片
UIPanGestureRecognizer * panGestureRecognizer = sender;
UIImageViewBaseClass* aImageView = [imageArray objectAtIndex:panGestureRecognizer.view.tag-101];
UIBezierPath * path = [UIBezierPath bezierPathWithRect:aImageView.imageFrame];
//跟新正在移動圖片的path
[pathArray replaceObjectAtIndex:panGestureRecognizer.view.tag-101 withObject:path];
}
break;
default:
break;
}
textView.textContainer.exclusionPaths = pathArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
3.下載原始碼_demo