2013-04-05 72 views
0

我正在做項目,我必須展示一張圖片,當我點擊圖片時,圖片上方會出現一個「白色視圖」。而當我雙擊「兩個別針」就會出現在「白色視圖」上。當我拉這兩個針時,白色視圖的寬度和高度會增加和減少。如何通過從iPhone中的UITapGestureRecognizer中的角度拖動視圖來增加視圖的大小和高度?

我無法做到這一點。

來自專家的任何想法都會受到高度的歡迎。

Second_View.h file 

#import <UIKit/UIKit.h> 

@interface Second_View : UIViewController<UIGestureRecognizerDelegate>{ 


UITapGestureRecognizer *tap; 
UISwipeGestureRecognizer *swipe; 
UIPanGestureRecognizer *pan; 
CGPoint startLocation; 
CGFloat lastScale; 
CGFloat firstX; 
CGFloat firstY; 
CGPoint lastLocation; 


UIImageView *imageVw; 
} 


@property (nonatomic) CGPoint center; 

@property (nonatomic) NSInteger count; 

@property(nonatomic,retain) UIImageView *imageVw; 


@end 


Second_View.m file 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
self.view.backgroundColor = [UIColor grayColor]; 

imageVw = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 500, 400)]; 
imageVw.image = [UIImage imageNamed:@"redacted2.jpg"]; 
imageVw.userInteractionEnabled=YES; 
imageVw.autoresizesSubviews = YES; 
imageVw.alpha = 0.93; // for opacity 
[self.view addSubview:imageVw]; 




tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
tap.numberOfTapsRequired = 1; 
[imageVw addGestureRecognizer:tap]; 
tap.delegate=self; 
pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; 
[pan setMinimumNumberOfTouches:1]; 
[pan setMaximumNumberOfTouches:2]; 
[pan setDelegate:self]; 
count=0; 
[imageVw addGestureRecognizer:pan]; 



} 


- (void)panAction:(UIPanGestureRecognizer *)gestureRecognizer { 
CGPoint translatedPoint = [(UIPanGestureRecognizer*)gestureRecognizer  translationInView:self.view]; 

if ([(UIPanGestureRecognizer*)gestureRecognizer state] ==  UIGestureRecognizerStateBegan) { 
firstX = [[gestureRecognizer view] center].x; 
firstY = [[gestureRecognizer view] center].y; 
} 


translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); 

[[gestureRecognizer view] setCenter:translatedPoint]; 

if ([(UIPanGestureRecognizer*)gestureRecognizer state] == UIGestureRecognizerStateEnded) { 
CGFloat velocityX = (0.2*[(UIPanGestureRecognizer*)gestureRecognizer  velocityInView:self.view].x); 


CGFloat finalX = translatedPoint.x + velocityX; 
CGFloat finalY = firstY;// translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y); 

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
if (finalX < 0) { 
//finalX = 0; 
} else if (finalX > 768) { 
//finalX = 768; 
} 

if (finalY < 0) { 
finalY = 0; 
} else if (finalY > 1024) { 
finalY = 1024; 
} 
} else { 
if (finalX < 0) { 
//finalX = 0; 
} else if (finalX > 1024) { 
//finalX = 768; 
} 

if (finalY < 0) { 
finalY = 0; 
} else if (finalY > 768) { 
finalY = 1024; 
} 
} 
} 
} 




-(void)handleTap:(UIPanGestureRecognizer *)gestureRecognizer 
{ 

CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view]; 
NSLog(@"x:%f y:%f",location.x,location.y); 


NSArray *subViewsOfImage = [imageVw subviews]; 


for (id subView in subViewsOfImage) { 
CGRect frame1=[subView frame]; 
int x=0; 
int y=0; 
x=frame1.origin.x+frame1.size.width; 
y=frame1.origin.y+frame1.size.height; 

if ((location.x>=frame1.origin.x && location.x<x) && (location.y>=frame1.origin.y && location.y<y)) 
{ 
NSLog(@"No"); 
return; 
} 
} 


derivedView *view=[[derivedView alloc]initWithFrame:CGRectMake(location.x, location.y, 100, 30)]; 
[view setBackgroundColor:[UIColor whiteColor]]; 
[imageVw addSubview: view]; 
NSArray * subViewsOfImage1 = [imageVw subviews]; 
NSLog(@"subViewsOfImage = %@",subViewsOfImage1); 
NSLog(@"Yes"); 

} 

derivedView.h文件

#import <UIKit/UIKit.h> 

@interface derivedView : UIView<UIGestureRecognizerDelegate> 
{ 
UIPanGestureRecognizer *pan; 
UITapGestureRecognizer *tap1; 
UITapGestureRecognizer *tap2; 
UITapGestureRecognizer *tap3; 
CGPoint lastLocation; 
CGFloat firstX; 
CGFloat firstY; 
UIImageView *rPinImgView; 
UIImageView *lPinImgView; 

} 

@end 

derivedView.m文件

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 

if (self) { 
// Initialization code 
pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; 
[pan setDelegate:self]; 
[self addGestureRecognizer:pan]; 
tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; 
[tap1 setDelegate:self]; 
[self addGestureRecognizer:tap1]; 

} 
return self; 
} 



- (void)tapAction:(UITapGestureRecognizer *)gestureRecognizer { 


//  lPinImgView=[[UIImageView alloc]initWithFrame:CGRectMake(-15,-20, 30, 30)]; 
// lPinImgView.image=[UIImage imageNamed:@"pin"]; 
// // pin.frame=CGRectMake(frame1.origin.x, frame1.origin.y-15, 10, 10); 
//  
// rPinImgView=[[UIImageView alloc]initWithFrame:CGRectMake  (self.frame.size.width-15 ,23, 30, 30)]; 
// //pin1.frame=CGRectMake(frame1.origin.x+frame1.size.width, frame1.origin.y+5, 10, 10); 
// rPinImgView.image=[UIImage imageNamed:@"pin1"]; 
// [self addSubview:lPinImgView]; 
// [self addSubview:rPinImgView]; 
NSArray *subViews=[self subviews]; 
NSLog(@"subViews%@",subViews); 
if ([subViews count]>0) { 
[lPinImgView removeFromSuperview]; 
[rPinImgView removeFromSuperview]; 
}else 
{ 
lPinImgView=[[UIImageView alloc]initWithFrame:CGRectMake(-15,-20, 30, 30)]; 
lPinImgView.image=[UIImage imageNamed:@"pin"]; 
// pin.frame=CGRectMake(frame1.origin.x, frame1.origin.y-15, 10, 10); 

rPinImgView=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-15 ,23, 30, 30)]; 
//pin1.frame=CGRectMake(frame1.origin.x+frame1.size.width, frame1.origin.y+5, 10, 10); 
rPinImgView.image=[UIImage imageNamed:@"pin1"]; 
tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector (rPinImageTap:)]; 
[rPinImgView addGestureRecognizer:tap2]; 
[tap2 setDelegate:self]; 
tap3=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lPinImageTap:)]; 
[rPinImgView addGestureRecognizer:tap3]; 
[tap3 setDelegate:self]; 
[self addSubview:lPinImgView]; 
[self addSubview:rPinImgView]; 

} 


- (void)lPinImageTap:(UIPanGestureRecognizer *)gestureRecognizer 
{ 

} 
- (void)rPinImageTap:(UIPanGestureRecognizer *)gestureRecognizer 
{ 

} 
- (void)panAction:(UIPanGestureRecognizer *)gestureRecognizer { 

CGPoint translatedPoint = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:self]; 

if ([(UIPanGestureRecognizer*)gestureRecognizer state] ==  UIGestureRecognizerStateBegan) { 
firstX = [[gestureRecognizer view] center].x; 
firstY = [[gestureRecognizer view] center].y; 
} 

// translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY); 
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); 

[[gestureRecognizer view] setCenter:translatedPoint]; 

if ([(UIPanGestureRecognizer*)gestureRecognizer state] == UIGestureRecognizerStateEnded) { 
CGFloat velocityX = (0.2*[(UIPanGestureRecognizer*)gestureRecognizer velocityInView:self].x); 


CGFloat finalX = translatedPoint.x + velocityX; 
CGFloat finalY = firstY;// translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y); 

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
if (finalX < 0) { 
//finalX = 0; 
} else if (finalX > 768) { 
//finalX = 768; 
} 

if (finalY < 0) { 
finalY = 0; 
} else if (finalY > 1024) { 
finalY = 1024; 
} 
} else { 
if (finalX < 0) { 
//finalX = 0; 
} else if (finalX > 1024) { 
//finalX = 768; 
} 

if (finalY < 0) { 
finalY = 0; 
} else if (finalY > 768) { 
finalY = 1024; 
} 
} 
} 
} 

}

回答

相關問題