在iPhone主屏幕中拖动类似UIView的应用程序
我有一个视图控件,在里面我计划放置一些控件,如按钮文本框等。。。我可以沿x轴拖动视图,如下所示: (一) (二) 使用以下代码:在iPhone主屏幕中拖动类似UIView的应用程序,iphone,objective-c,ipad,uiview,drag,Iphone,Objective C,Ipad,Uiview,Drag,我有一个视图控件,在里面我计划放置一些控件,如按钮文本框等。。。我可以沿x轴拖动视图,如下所示: (一) (二) 使用以下代码: -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if( [touch view] == ViewMain) { CGPoint locati
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
displaceX = location.x - ViewMain.center.x;
displaceY = ViewMain.center.y;
startPosX = location.x - displaceX;
}
CurrentTime = [[NSDate date] timeIntervalSince1970];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - displaceX;
location.y = displaceY;
ViewMain.center = location;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
double time = [[NSDate date] timeIntervalSince1970]-CurrentTime;
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - displaceX;
location.y = displaceY;
ViewMain.center = location;
double speed = (ViewMain.center.x-startPosX)/(time*2);
NSLog(@"speed: %f", speed);
}
}
并不是说我必须添加全局变量:
float displaceX = 0;
float displaceY = 0;
float startPosX = 0;
float startPosY = 0;
double CurrentTime;
我创建这些变量的原因是,当我开始拖动视图时,视图从我接触它的点移动,而不是从中间移动
无论如何,如果我触摸按钮或图像,视图将不会拖动,即使图像在背景上具有透明度。我希望仍然能够拖动视图,无论视图顶部是否有图像。我在想,也许我需要在所有东西的顶部放置一个大的透明视图,但我需要按钮、图像等。我希望能够像使用以下工具一样拖动视图:
请注意,无论我第一次触摸应用程序/图像或文本是什么,我都可以拖动视图。我该怎么办? 你可能想考虑一个不同的方法来解决这个问题。与其自己手动管理滚动内容,不如使用
UIScrollView
,将paginabled
属性设置为YES。这是苹果公司推荐的方法(可能是Springboard.app在您上次截图中使用的方法)。如果您是iOS开发者计划的成员,请查看UIScrollView上的WWDC 2010会话,以了解此示例。我想他们可能也在developer.apple.com上发布了示例代码。我想你的问题是,如果你触摸一个UIButton或一个启用了交互功能的UIImageView,它不会传递触摸信息。
对于图像,取消选中IB中的已启用用户交互
属性。
对于导致TouchsBegind:withEvent:等未被调用的按钮,请查看以下链接: