我有一個觀點的控制和內部應用程序我打算把如按鈕文本框等一些控件...我可以沿x軸拖我的看法一樣:拖動的UIView喜歡在主屏幕iPhone
1)
2)
用下面的代碼:
-(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;
爲什麼我創建的那些變量的原因是,當我開始拖動的觀點認爲從點上移動時,我觸摸它,而不是從中間。
無論如何,如果我觸摸按鈕或圖像,即使圖像在背景上具有透明度,視圖也不會拖動。我希望能夠繼續拖動視圖,而不管視圖頂部是否有圖像。我在那裏想,也許我需要把一切之上的大型透明的看法,但我需要有按鈕,圖片等等。我希望能夠拖動一個視圖只是像您可以:
請注意,無論我第一次觸摸應用/圖片還是文本,我都能夠拖動該視圖。我怎麼能這樣做?