Iphone 如何在iOS中移动ResizeableUIView?
我想从其角落拖动可调整大小的UIView? 就像在桌面上一样,我们单击鼠标按钮并按住鼠标,然后拖动鼠标,这样就可以看到一个透明的视图 我想在我的iPhone项目中使用它。我希望当用户触摸并拖动手指时,可以看到一个可调整大小的透明视图 到目前为止,我已经完成了点击按钮,视图是可见的,并且可以调整大小。但这不是我的解决方案,我希望在任何iphone屏幕上都能触摸到可调整大小的UIView 欢迎提出任何意见和建议 这是我迄今为止所做的代码:Iphone 如何在iOS中移动ResizeableUIView?,iphone,ios,objective-c,xcode,uiview,Iphone,Ios,Objective C,Xcode,Uiview,我想从其角落拖动可调整大小的UIView? 就像在桌面上一样,我们单击鼠标按钮并按住鼠标,然后拖动鼠标,这样就可以看到一个透明的视图 我想在我的iPhone项目中使用它。我希望当用户触摸并拖动手指时,可以看到一个可调整大小的透明视图 到目前为止,我已经完成了点击按钮,视图是可见的,并且可以调整大小。但这不是我的解决方案,我希望在任何iphone屏幕上都能触摸到可调整大小的UIView 欢迎提出任何意见和建议 这是我迄今为止所做的代码: - (void)viewDidLoad { tap =
- (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;
}
首先,必须声明两个全局变量
CGPoint startPoint;
BOOL _shouldResize;
然后,您必须覆盖视图中给定的两个触摸事件方法
1.接触开始了
- (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.触摸移动
- (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];
}
希望这对您有所帮助:先生,这不是我想要的。我有这个演示。如果您有任何想法或建议,请告诉我。尝试此CGPoint touchPoint=[[touchs anyObject]locationInView:[self superview]];//Super view not Found您必须在视图中实现此方法,该视图必须调整大小。不在viewcontroller或任何其他类中。要实现这些方法,您必须创建新的视图类及其.m粘贴代码,并创建新类的新实例,而不是UIView。好吗?先生,还有一个问题,我可以从触摸的位置开始查看吗?在我的代码中,我给出UIView.frame=cgrectmake100200,0,0;而不是自我。所以我的观点从100200,0,0开始;所以现在我想从那里开始。有可能吗?如果您将宽度和高度设为0,则不会显示任何视图。你必须做的是,用最大尺寸或最小尺寸(宽度和高度不为0,0)初始化视图,好吗?首先让用户通过触摸调整其大小并在其角上移动,好吗?是的,当用户触摸并拖动手指时,没有视图。视图将出现,并在用户未触摸屏幕的位置结束。和uiview从用户触摸屏幕的位置开始。