2013-05-03 28 views
1

我想拖動可調整大小的UIView從它的角落? 像在桌面上我們點擊鼠標按鈕並按住並點擊並拖動鼠標,然後透明視圖就可以看到像這樣。如何從iOS的角落移動resizableUIView?

我想在我的項目在iPhone。我希望當用戶觸摸並拖動手指時可以看到一個可調整大小的透明視圖。

我已經完成了點擊按鈕,視圖是可見的,也是可以調整大小的。但這不是我的解決方案,我想要在任何iPhone屏幕上觸及可調整大小的UIView。

任何想法和建議將非常受歡迎。

這是我的代碼是我迄今所做的:

- (void)viewDidLoad 
{ 


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; 
// [pan minimumNumberOfTouches:1]; 
// [pan maximumNumberOfTouches:2]; 
[imageVw addGestureRecognizer:pan]; 
} 



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

userResizableView = [[SPUserResizableView alloc] init]; 
userResizableView.frame = CGRectMake(x,y,w,h); 
//UIView *content_View = [[UIView alloc] initWithFrame:userResizableView.bounds]; 
//[content_View setBackgroundColor:[UIColor whiteColor]]; 
imageVw1 = [[UIImageView alloc]initWithFrame:userResizableView.bounds]; 
//imageVw1.image = [UIImage imageNamed:@"redacted2.jpg"]; 
imageVw1.backgroundColor = [UIColor blackColor]; 
imageVw1.userInteractionEnabled=YES; 
imageVw1.autoresizesSubviews = YES; 
imageVw1.alpha = 0.13; // for opacity 
userResizableView.contentView = imageVw1; 
//[content_View addSubview:imageVw1]; 

//userResizableView.contentView = content_View; 
userResizableView.delegate = self; 
[userResizableView showEditingHandles]; 
currentlyEditingView = userResizableView; 
lastEditedView = userResizableView; 

// [self.view addSubview:userResizableView]; 
// [imageVw addSubview: dview]; 
[self.view addSubview: userResizableView]; 
[userResizableView release]; 

} 

- (void)userResizableViewDidBeginEditing:(SPUserResizableView *)userResizableView1 { 

width = userResizableView.frame.size.width; 
height = userResizableView.frame.size.height; 
width1 = width; 
height1 = height; 
width = width + width; 
height = height + height; 
[currentlyEditingView hideEditingHandles]; 
currentlyEditingView = userResizableView1; 


} 

- (void)userResizableViewDidEndEditing:(SPUserResizableView *)userResizableView1 { 

lastEditedView = userResizableView1; 
x1 = x; 
y1 = y; 
x = userResizableView.frame.origin.x; 
y = userResizableView.frame.origin.y; 

} 
+0

先生,這不是我想要的。我有這個演示。如果您有任何想法或建議,請告訴我。 – 2013-05-03 08:41:46

+0

試試這個https://github.com/bitmantra1/ResizeImage – 2013-05-03 10:28:31

回答

0

首先必須聲明兩個全局變量

CGPoint startPoint; 
BOOL _shouldResize; 

然後,你必須覆蓋在你看來是兩個觸控事件的方法鑑於

1.TouchesBegan

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

CGPoint touchPoint = [[touches anyObject] locationInView:[self superview]]; 

/* 

Here you have to check user tap to the corner of your view by using the method 
CGRectContainsPoint(CGRect rect, touchPoint); 

Here the parameter rect should be the corner of your view 

if(!CGRectContainsPoint(CGRect rect, touchPoint)) { // Here user did not tap in your specific rect. so no need of resizing 

    _shouldResize = NO; 
    return; 
} 

*/ 

_shouldResize = YES; 

startPoint = touchPoint; 

} 

2.TouchesMoved

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

if(!_shouldResize) 
    return; 

CGPoint touchPoint = [[touches anyObject] locationInView:[self superview]]; 

CGFloat displacementX = touchPoint.x - startPoint.x; 
CGFloat displacementY = touchPoint.y - startPoint.y; 

startPoint.x = touchPoint.x; 
startPoint.y = touchPoint.y; 

CGRect frame = [self frame]; 
frame.size.width += displacementX; 
frame.size.height += displacementY; 

[self setFrame:frame]; 

} 

希望這將幫助你:)

+0

CGPoint touchPoint = [[touches anyObject] locationInView:[self superview]]; //超級視圖未找到 – 2013-05-03 11:11:02

+0

您必須在您的視圖中實施此方法,該方法必須調整大小。不在viewcontroller或任何其他類。爲了實現這些方法,你已經創建了新的視圖類和它的.m粘貼代碼,並創建新類的新實例,而不是UIView好嗎? – 2013-05-03 11:18:38

+0

謝謝Aloot先生。 – 2013-05-03 11:21:53