我们如何垂直向上和向下移动视图? [英] How can we move a view vertically upward and downwards?
问题描述
我在主视图中有一个tableview,我需要通过触摸垂直移动此tableview。
I have a tableview in my main view, and I need to move this tableview vertically with touch.
我想在触摸时向上和向下拖动此tableview任何位置,当我的手指向上或向下移动时,桌面视图将根据触摸移动。
I want to drag this tableview upwards and downwards when touching at any position, and when my finger moves upward or downward the tableview will move according to the touch.
我的主视图中有很多控制器,所以我想触摸仅限tableview。
I have lots of controllers in my main view, so i want to touch the tableview only.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
如何在上述问题中使用这些代表?
How do I use these delegates in the question above?
提前致谢。
推荐答案
你刚刚上课,继承自UITableview
YOu just make a class , inherited from UITableview
创建一个类Drag
Drag.h应该是这样的
Drag.h should be like this
@interface Drag : UITableView {
CGPoint touchingPoint;
}
并通过包含以下方法修改Drag.m文件
and Modify Drag.m File by including following methods
Drag.m
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
touchingPoint = pt;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[UITableView beginAnimations:nil context:nil];
[UITableView setAnimationDuration:0.1];
CGRect newFrame = self.frame;
newFrame.origin.y += pt.y - touchingPoint.y;
[self setFrame:newFrame];
[UITableView commitAnimations];
}
就是这样。
此外,在您的 viewDidLoad
方法中,
把这些行
Drag *objDrag = [[Drag alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
objDrag.delegate = self;
objDrag.dataSource = self;
[objDrag setUserInteractionEnabled:YES];
[self.view addSubview:objDrag];
请记住:要垂直拖动,否则必须触摸表格的分隔符它将考虑触摸单元格并将滚动而不是拖动。
Remember: for dragging vertically , you must touch the separators of table otherwise it will consider touch to cell and will scroll instead of dragging.
这篇关于我们如何垂直向上和向下移动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!