344 lines
11 KiB
Objective-C
344 lines
11 KiB
Objective-C
//
|
|
// ShiPingEditShuiYinView.m
|
|
// ProductApp
|
|
//
|
|
// Created by 工作 on 2025/4/9.
|
|
//
|
|
|
|
#import "ShiPingEditShuiYinView.h"
|
|
#import "UIImageView+VideoImageView.h"
|
|
#import "WYCamaImageTools.h"
|
|
|
|
@interface ShiPingEditShuiYinView ()<WYCamaImageToolsDelegate>
|
|
///
|
|
@property (nonatomic , strong) UIImage *imageEdit;
|
|
///
|
|
@property (nonatomic , strong) UIView *viewedit;
|
|
|
|
///
|
|
@property (nonatomic , strong) UIView *viewboy;
|
|
@property (nonatomic , assign) CGPoint transLast;
|
|
|
|
///
|
|
@property (nonatomic , strong) NSMutableArray *arrItems;
|
|
|
|
///
|
|
@property (nonatomic , strong) UIImageView *imgvNow;
|
|
@property (nonatomic, strong) WYCamaImageTools *tools;
|
|
|
|
@end
|
|
|
|
@implementation ShiPingEditShuiYinView
|
|
|
|
-(id)initWithFrame:(CGRect)frame
|
|
{
|
|
if(self = [super initWithFrame:frame])
|
|
{
|
|
[self setUserInteractionEnabled:YES];
|
|
|
|
|
|
}
|
|
return self;
|
|
}
|
|
-(void)setUrlvideo:(NSURL *)urlvideo
|
|
{
|
|
_urlvideo = urlvideo;
|
|
|
|
[UIImageView videoImageWithvideoURL:self.urlvideo atTime:0.1 back:^(UIImage * _Nonnull image) {
|
|
if(image)
|
|
{
|
|
self.imageEdit = image;
|
|
float f_w = UISCREEN_WIDTH;
|
|
float f_h = UISCREEN_HEIGHT-TabHeight-NavHeight-80;
|
|
if(f_w/f_h>image.size.width/image.size.height)
|
|
{
|
|
f_w = f_h*image.size.width/image.size.height;
|
|
}
|
|
else
|
|
{
|
|
f_h = f_w*image.size.height/image.size.width;
|
|
}
|
|
if(self.viewedit)
|
|
{
|
|
[self.viewedit mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.center.equalTo(self);
|
|
make.width.offset(f_w);
|
|
make.height.offset(f_h);
|
|
}];
|
|
}
|
|
}
|
|
}];
|
|
}
|
|
|
|
-(UIView *)drawKuangView:(int)type
|
|
{
|
|
UIView *viewboy= [[UIView alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];
|
|
[viewboy setBackgroundColor:[UIColor clearColor]];
|
|
[self.viewedit addSubview:viewboy];
|
|
[viewboy setClipsToBounds:NO];
|
|
[viewboy.layer setBorderWidth:1.5];
|
|
[viewboy.layer setBorderColor:[UIColor whiteColor].CGColor];
|
|
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMoveAction:)];
|
|
[viewboy addGestureRecognizer:pan];
|
|
|
|
if(type==1)
|
|
{
|
|
UITextField *field = [[UITextField alloc] init];
|
|
[field setTextColor:RGBACOLOR(255, 255, 255,0.9)];
|
|
[field setTextAlignment:NSTextAlignmentCenter];
|
|
[field setFont:[UIFont systemFontOfSize:15]];
|
|
[field setPlaceholder:@"请输入"];
|
|
[viewboy addSubview:field];
|
|
[field mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(viewboy);
|
|
}];
|
|
[field setTag:10];
|
|
}
|
|
else if(type==2)
|
|
{
|
|
UILabel *lbname = [[UILabel alloc] init];
|
|
[lbname setText:@"点击添加图片"];
|
|
[lbname setTextColor:RGBACOLOR(255, 255, 255,0.9)];
|
|
[lbname setTextAlignment:NSTextAlignmentCenter];
|
|
[lbname setFont:[UIFont systemFontOfSize:15]];
|
|
[viewboy addSubview:lbname];
|
|
[lbname mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(viewboy);
|
|
}];
|
|
UIImageView *imgvshow = [[UIImageView alloc] init];
|
|
[imgvshow setContentMode:UIViewContentModeScaleAspectFill];
|
|
[viewboy addSubview:imgvshow];
|
|
[imgvshow mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(viewboy);
|
|
}];
|
|
[imgvshow setClipsToBounds:YES];
|
|
[imgvshow setUserInteractionEnabled:YES];
|
|
[imgvshow setTag:10];
|
|
UIButton *btaddimg = [[UIButton alloc] init];
|
|
[imgvshow addSubview:btaddimg];
|
|
[btaddimg mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(imgvshow);
|
|
}];
|
|
[btaddimg addTarget:self action:@selector(addimgAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
|
|
///拖动放大缩小
|
|
UIView *viewbit = [[UIView alloc] init];
|
|
[viewboy addSubview:viewbit];
|
|
[viewbit mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.height.offset(60);
|
|
make.right.equalTo(viewboy).offset(30);
|
|
make.bottom.equalTo(viewboy).offset(30);
|
|
}];
|
|
[viewbit setTag:1];
|
|
|
|
UIImageView *imgvbig = [[UIImageView alloc] init];
|
|
[imgvbig setImage:[UIImage imageNamed:@"video_edit_big"]];
|
|
[imgvbig setUserInteractionEnabled:YES];
|
|
[viewbit addSubview:imgvbig];
|
|
[imgvbig mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.height.offset(20);
|
|
make.center.equalTo(viewbit);
|
|
}];
|
|
[imgvbig setUserInteractionEnabled:YES];
|
|
UIPanGestureRecognizer *panBig = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTwoGestureAction:)];
|
|
[viewbit addGestureRecognizer:panBig];
|
|
|
|
UIButton *btdel = [[UIButton alloc] init];
|
|
[btdel setImage:[UIImage imageNamed:@"video_edit_X"] forState:UIControlStateNormal];
|
|
[viewboy addSubview:btdel];
|
|
[btdel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.height.offset(40);
|
|
make.left.equalTo(viewboy.mas_left).offset(-20);
|
|
make.top.equalTo(viewboy.mas_top).offset(-20);
|
|
}];
|
|
[btdel addTarget:self action:@selector(delAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
[btdel setTag:2];
|
|
|
|
|
|
return viewboy;
|
|
}
|
|
-(void)addimgAction:(UIButton *)sender
|
|
{
|
|
self.imgvNow = (UIImageView *)sender.superview;
|
|
[self.tools choosePicWithViewController:self.viewController chooseType:ChooseTypeAlbum];
|
|
}
|
|
-(void)didEndChoosePic:(UIImage *)image
|
|
{
|
|
self.imgvNow.image = image;
|
|
}
|
|
///1文字 2图片
|
|
-(void)addType:(int)type
|
|
{
|
|
if(!self.viewedit)
|
|
{
|
|
UIView *viewedit = [[UIView alloc] init];
|
|
[self addSubview:viewedit];
|
|
[viewedit setUserInteractionEnabled:YES];
|
|
_viewedit = viewedit;
|
|
self.arrItems = [NSMutableArray new];
|
|
|
|
if(self.imageEdit)
|
|
{
|
|
float f_w = UISCREEN_WIDTH;
|
|
float f_h = UISCREEN_HEIGHT-TabHeight-NavHeight-80;
|
|
if(f_w/f_h>self.imageEdit.size.width/self.imageEdit.size.height)
|
|
{
|
|
f_w = f_h*self.imageEdit.size.width/self.imageEdit.size.height;
|
|
}
|
|
else
|
|
{
|
|
f_h = f_w*self.imageEdit.size.height/self.imageEdit.size.width;
|
|
}
|
|
[self.viewedit mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.center.equalTo(self);
|
|
make.width.offset(f_w);
|
|
make.height.offset(f_h);
|
|
}];
|
|
}
|
|
}
|
|
UIView *viewitem = [self drawKuangView:type];
|
|
[self.arrItems addObject:viewitem];
|
|
}
|
|
-(void)setItemLayerHidden
|
|
{
|
|
for(UIView *view in self.arrItems)
|
|
{
|
|
[view.layer setBorderColor:[UIColor clearColor].CGColor];
|
|
|
|
[[view viewWithTag:1] setHidden:YES];
|
|
[[view viewWithTag:2] setHidden:YES];
|
|
}
|
|
}
|
|
-(CALayer *)getShowLayer
|
|
{
|
|
|
|
for(UIView *view in self.arrItems)
|
|
{
|
|
[view.layer setBorderColor:[UIColor clearColor].CGColor];
|
|
|
|
[[view viewWithTag:1] setHidden:YES];
|
|
[[view viewWithTag:2] setHidden:YES];
|
|
}
|
|
|
|
float fblv = self.imageEdit.size.width/self.viewedit.size.width;
|
|
|
|
UIImage *imagetemp = [self imageFromView:self.viewedit];
|
|
UIImageView *imgvback = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.viewedit.width*fblv, self.viewedit.height*fblv)];
|
|
[imgvback setImage:imagetemp];
|
|
|
|
for(UIView *view in self.arrItems)
|
|
{
|
|
[view.layer setBorderColor:[UIColor whiteColor].CGColor];
|
|
|
|
[[view viewWithTag:1] setHidden:NO];
|
|
[[view viewWithTag:2] setHidden:NO];
|
|
}
|
|
|
|
return imgvback.layer;
|
|
}
|
|
|
|
//生成图片
|
|
- (UIImage *)imageFromView:(UIView *)view {
|
|
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);///第二个参数是设置是否透明
|
|
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
|
|
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext();
|
|
return snapshotImage;
|
|
}
|
|
|
|
-(void)delAction:(UIButton *)sender
|
|
{
|
|
[sender.superview removeFromSuperview];
|
|
}
|
|
//二次缩放调用
|
|
- (void)panTwoGestureAction:(UIPanGestureRecognizer *)pan{
|
|
|
|
if(pan.state == UIGestureRecognizerStateBegan){
|
|
CGPoint transP = [pan locationInView:self.viewedit];
|
|
self.viewboy = pan.view.superview;
|
|
self.transLast = transP;
|
|
|
|
}else if(pan.state == UIGestureRecognizerStateChanged){
|
|
|
|
CGPoint transP = [pan translationInView:self.viewedit];
|
|
|
|
self.viewboy.width += transP.x;
|
|
|
|
if(self.viewboy.width<20)self.viewboy.width=20;
|
|
|
|
self.viewboy.height += transP.y;
|
|
if(self.viewboy.height<20)self.viewboy.height=20;
|
|
|
|
[self kuangBianJie:NO];
|
|
|
|
[pan setTranslation:CGPointZero inView:self.viewedit];
|
|
}else if(pan.state == UIGestureRecognizerStateEnded){
|
|
self.transLast = self.viewboy.frame.origin;
|
|
}
|
|
}
|
|
///框边界判断
|
|
-(void)kuangBianJie:(BOOL)ismove
|
|
{
|
|
if(ismove)
|
|
{///移动的判断
|
|
if(self.viewboy.origin.x+self.viewboy.size.width>self.viewedit.width)
|
|
{
|
|
self.viewboy.right = self.viewedit.width;
|
|
}
|
|
if(self.viewboy.origin.y+self.viewboy.size.height>self.viewedit.height)
|
|
{
|
|
self.viewboy.bottom = self.viewedit.height;
|
|
}
|
|
}
|
|
else
|
|
{///绘制的判断
|
|
if(self.viewboy.origin.x+self.viewboy.size.width>self.viewedit.width)
|
|
{
|
|
self.viewboy.width = self.viewedit.width -self.viewboy.origin.x;
|
|
}
|
|
if(self.viewboy.origin.y+self.viewboy.size.height>self.viewedit.height)
|
|
{
|
|
self.viewboy.height = self.viewedit.height -self.viewboy.origin.y;
|
|
}
|
|
}
|
|
|
|
|
|
if(self.viewboy.origin.x<0)
|
|
{
|
|
self.viewboy.frame = CGRectMake(0, self.viewboy.origin.y, self.viewboy.width, self.viewboy.height);
|
|
}
|
|
if(self.viewboy.origin.y<0)
|
|
{
|
|
self.viewboy.frame = CGRectMake(self.viewboy.origin.x, 0, self.viewboy.width, self.viewboy.height);
|
|
}
|
|
}
|
|
|
|
///移动
|
|
-(void)panMoveAction:(UIPanGestureRecognizer *)pan{
|
|
|
|
if(pan.state == UIGestureRecognizerStateBegan){
|
|
self.viewboy = pan.view;
|
|
}else if(pan.state == UIGestureRecognizerStateChanged){
|
|
|
|
CGPoint transP = [pan translationInView:self.viewboy];
|
|
self.viewboy.transform = CGAffineTransformTranslate(self.viewboy.transform, transP.x, transP.y);
|
|
|
|
[self kuangBianJie:YES];
|
|
|
|
[pan setTranslation:CGPointZero inView:self.viewboy];
|
|
|
|
}else if(pan.state == UIGestureRecognizerStateEnded){
|
|
|
|
}
|
|
}
|
|
|
|
- (WYCamaImageTools *)tools {
|
|
if (!_tools) {
|
|
_tools = [[WYCamaImageTools alloc] init];
|
|
_tools.delegate = self;
|
|
}return _tools;
|
|
}
|
|
|
|
@end
|