1

我想从它的角落拖动可调整大小的 UIView?就像在桌面上一样,我们单击鼠标按钮并按住单击并拖动鼠标,然后可以看到像这样的透明视图。

我想在我的 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;

}
4

1 回答 1

0

首先你必须声明两个全局变量

CGPoint startPoint;
BOOL _shouldResize;

然后,您必须按照给定的方式覆盖视图中的两个触摸事件方法

1.TouchesBegin

- (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];

}

希望对你有帮助 :)

于 2013-05-03T11:06:11.143 回答