UIpangesture로 UIView 옮기기& 특정 프레임 넘어가지 않게 설정
소스
#import "ViewController.h"
@interface ViewController (){
//사진틀
IBOutlet UIImageView *myImageView;
//뷰파인더를 옮기기 전 위치
CGPoint beforPoint;
//뷰파인더
UIView *viewFinder;
//뷰파인더를 감싸고 있는 네 개의 점.
UIView *leftTopPoint;
UIView *rightTopPoint;
UIView *leftDownPoint;
UIView *rightDownPoint;
UIBezierPath *blackPath; //검은색 블러
UIBezierPath *framePath; //포커스
CAShapeLayer *fillLayer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.
blackPath = [UIBezierPath bezierPathWithRect:self.view.frame]; //흐리게 블러 처리할 부분 : View 전체 지정
//이미지뷰의 제일 왼쪽 모서리 구하기
float startXpoint = myImageView.frame.origin.x;
float startYpoint = myImageView.frame.origin.y;
//뷰파인더
viewFinder = [[UIView alloc] initWithFrame:CGRectMake(startXpoint, startYpoint, 100, 100)];
viewFinder.backgroundColor = [UIColor clearColor];
//viewFinder.center = self.view.center;
viewFinder.layer.borderColor = [UIColor lightGrayColor].CGColor;
viewFinder.layer.borderWidth= 1.0f;
[self.view addSubview:viewFinder];
framePath = [UIBezierPath bezierPathWithRect:viewFinder.frame];
[blackPath appendPath:framePath];
blackPath.usesEvenOddFillRule = YES;
//오버레이뷰 : 약간 어둡게
fillLayer = [CAShapeLayer layer];
fillLayer.path = blackPath.CGPath; //1.번에서 지정한 범위 그대로 설정
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor blackColor].CGColor; //검은색 바탕
fillLayer.opacity = 0.4; //약간 흐리게
[self.view.layer addSublayer:fillLayer]; //루트뷰 레이어에 오버레이뷰 추가
//뷰파인더 위치 이동 제스쳐 이벤트
UIPanGestureRecognizer *panResizeGestureViewFinder =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(resizeTranslateViewFinder:)];
[viewFinder addGestureRecognizer:panResizeGestureViewFinder];
}
//파인더뷰 드래그
-(void)resizeTranslateViewFinder:(UIPanGestureRecognizer *) recognizer{
//드래그 후 위치값
CGPoint afterPoint = [recognizer locationInView:viewFinder.superview];
float finderX = viewFinder.frame.origin.x;
float finderY = viewFinder.frame.origin.y;
// float beforX = beforPoint.x;
// float beforY = beforPoint.y;
float afterX = afterPoint.x;
float afterY = afterPoint.y;
// NSLog(@"finderX : %f , finderY: %f" ,finderX , finderY);
// NSLog(@"beforX : %f , beforY: %f" ,beforX , beforY);
// NSLog(@"afterX : %f , afterY: %f" ,afterX , afterY);
//
//
//
if ([recognizer state] == UIGestureRecognizerStateBegan) {
NSLog(@"시작");
viewFinder.center = afterPoint; //뷰파인더 재할당
[self setTransParentViewFinderSetting]; //뷰파인더 셋팅
beforPoint = [recognizer locationInView:viewFinder.superview];
//[viewFinder setNeedsDisplay];
}else if([recognizer state] == UIGestureRecognizerStateChanged){
NSLog(@"중간");
//뷰 파인더가 이미지 뷰 가이드라인 넘어가지 않게 설정
[self actionViewFinderGuideLine:finderX and:finderY and:afterX and:afterY and :afterPoint];
}else if ([recognizer state] == UIGestureRecognizerStateEnded ){
NSLog(@"종료");
//뷰 파인더가 이미지 뷰 가이드라인 넘어가지 않게 설정
[self actionViewFinderGuideLine:finderX and:finderY and:afterX and:afterY and :afterPoint];
}
};
////뷰파인더(투명색) 위치 설정을 위한 메소드
-(void) setTransParentViewFinderSetting {
//검은색배경 위치와 오버레이뷰 해제
[blackPath removeAllPoints];
[fillLayer removeFromSuperlayer];
//검은색 바탕화면 다시 셋팅
blackPath = [UIBezierPath bezierPathWithRect:self.view.frame];
//드래그된 뷰파인더 위치를 framepath에(위치경로) 대입
framePath = [UIBezierPath bezierPathWithRect:viewFinder.frame];
//뷰파인더(투명) 경로를 검은색 바탕화면 경로에 더해주기
[blackPath appendPath:framePath];
blackPath.usesEvenOddFillRule = YES;
//오버레이뷰 : 약간 어둡게
fillLayer = [CAShapeLayer layer];
fillLayer.path = blackPath.CGPath; //1.번에서 지정한 범위 그대로 설정
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor blackColor].CGColor; //검은색 바탕
fillLayer.opacity = 0.4; //약간 흐리게
[self.view.layer addSublayer:fillLayer]; //루트뷰 레이어에 오버레이뷰 추가
}
//뷰 파인더가 이미지 뷰 가이드라인 넘어가지 않게 설정
-(void)actionViewFinderGuideLine : (float) finderX and:(float)finderY and:(float)afterX and:(float)afterY and:(CGPoint)afterPoint{
//*왼쪽테두리 넘지 못함
if(finderX <= 0.0){
viewFinder.center = CGPointMake(50, afterY);
[self setTransParentViewFinderSetting];
//왼쪽테두리 + 위쪽 테두리 넘지 못한다.
if(finderY <= myImageView.frame.origin.y){
viewFinder.center = CGPointMake(50, myImageView.frame.origin.y + 50);
[self setTransParentViewFinderSetting];
}
//왼쪽테두리 + 아래쪽 테두리 넘지 못함.
if((finderY+100) >= myImageView.frame.origin.y+myImageView.frame.size.height){
viewFinder.center = CGPointMake(50,( myImageView.frame.origin.y+myImageView.frame.size.height) - 50);
[self setTransParentViewFinderSetting];
}
//*위쪽 테두리 넘지 못한다.
}else if(finderY <= myImageView.frame.origin.y){
viewFinder.center = CGPointMake(afterX, myImageView.frame.origin.y + 50);
[self setTransParentViewFinderSetting];
//위쪽 테두리 + 오른쪽 테두리 넘지 못함
if((finderX+100) >= myImageView.frame.size.width){
viewFinder.center = CGPointMake(myImageView.frame.size.width - 50, myImageView.frame.origin.y + 50);
[self setTransParentViewFinderSetting];
}
//위쪽 테두리 + 왼쪽 테두리 넘지 못함
if(finderX <= 0.0){
viewFinder.center = CGPointMake(50, afterY);
[self setTransParentViewFinderSetting];
}
//*오른쪽 테두리 넘지 못한다.
}else if((finderX+100) >= myImageView.frame.size.width){
viewFinder.center = CGPointMake(myImageView.frame.size.width - 50, afterY);
[self setTransParentViewFinderSetting];
//오른쪽 테두리 + 위쪽 테두리 넘지 못함
if(finderY <= myImageView.frame.origin.y){
viewFinder.center = CGPointMake(myImageView.frame.size.width - 50, myImageView.frame.origin.y + 50);
[self setTransParentViewFinderSetting];
}
//오른쪽 테두리 + 아래쪽 테두리 넘지 못함
if((finderY+100) >= myImageView.frame.origin.y+myImageView.frame.size.height){
viewFinder.center = CGPointMake(myImageView.frame.size.width - 50,( myImageView.frame.origin.y+myImageView.frame.size.height) - 50);
[self setTransParentViewFinderSetting];
}
//*아래쪽 테두리 넘지 못함
}else if((finderY+100) >= myImageView.frame.origin.y+myImageView.frame.size.height){
viewFinder.center = CGPointMake(afterX,( myImageView.frame.origin.y+myImageView.frame.size.height) - 50);
[self setTransParentViewFinderSetting];
//아래쪽 + 왼쪽 테두리 넘지 못함
if(finderX <= 0.0){
viewFinder.center = CGPointMake(50, afterY);
[self setTransParentViewFinderSetting];
}
//아래쪽 + 오른쪽 테두리 넘지 못함
if((finderX+100) >= myImageView.frame.size.width){
viewFinder.center = CGPointMake(myImageView.frame.size.width - 50, afterY);
[self setTransParentViewFinderSetting];
}
}else{
viewFinder.center = afterPoint; //뷰파인더 재할당
[self setTransParentViewFinderSetting];
// [viewFinder setNeedsDisplay];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
'ios 뽀개기 > objective-c' 카테고리의 다른 글
엔터프라이즈 계정으로 배포 (0) | 2019.02.15 |
---|---|
이미지 자르기 (0) | 2019.01.27 |
일정시간이 지난 파일 자동 삭제하기 (0) | 2019.01.09 |
날짜데이터를 문자로 변환 (0) | 2019.01.09 |
AppDelegate 사용자 터치 감지하기 (0) | 2019.01.04 |
댓글