如何在iPhone上的TouchsMoved方法中将控件从一个uiview切换到另一个uiview
大家好,我是iPhone开发人员中的新手。请任何人告诉我,如果我有两个视图,一个在另一个上,我希望当我单击“超级视图”时,它会拖动,当我单击“子视图”时,它会拖动。有人能帮我吗 这是我的密码:如何在iPhone上的TouchsMoved方法中将控件从一个uiview切换到另一个uiview,iphone,cocoa-touch,uiview,Iphone,Cocoa Touch,Uiview,大家好,我是iPhone开发人员中的新手。请任何人告诉我,如果我有两个视图,一个在另一个上,我希望当我单击“超级视图”时,它会拖动,当我单击“子视图”时,它会拖动。有人能帮我吗 这是我的密码: -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *aTouch = [touches anyObject]; //UITouch *aTouchSuper = [touches anyOb
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
//UITouch *aTouchSuper = [touches anyObject];
CGPoint location = [aTouch locationInView:self.view];
//CGPoint locationSuper=[aTouchSuper locationInView:viewForTouch];
[UIView beginAnimations:@"Dragging A DraggableView" context:nil];
////////////////////////
//if(location.x>50 &&location.x<300 ){
// viewForTouch.frame = CGRectMake(location.x,0,
// viewForTouch.frame.size.width, viewForTouch.frame.size.height);
//
//
//
// }
UIView *subview = [self.view hitTest:[[touches anyObject] locationInView:self.view] withEvent:nil];
NSLog(@"%i",subview.tag);
if (location.x <= 50)
{
location.x = 50;
}
if (location.x >= 300)
{
location.x = 300;
}
if(subview.tag==12){
viewForTouchInner.frame = CGRectMake(location.x,0,
viewForTouchInner.frame.size.width, viewForTouchInner.frame.size.height);
}
if(subview.tag==11)
{
viewForTouch.frame = CGRectMake(location.x,0,
viewForTouch.frame.size.width, viewForTouch.frame.size.height);
}
///////////////////////
//if(viewForTouchInner.hidden=FALSE){
//
//
//
//
// location.x=0;
// NSLog(@"%f",location.x);
// viewForTouchInner.frame = CGRectMake(location.x,0,
//
// viewForTouch.frame.size.width, viewForTouch.frame.size.height);
//
//
// }
[UIView commitAnimations];
[buttonForMove addTarget:self action:@selector(Show) forControlEvents:UIControlEventTouchUpInside];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*aTouch=[触摸任何对象];
//UITouch*aTouchSuper=[触摸任何对象];
CGPoint location=[aTouch locationInView:self.view];
//CGPoint locationSuper=[aTouchSuper locationInView:viewForTouch];
[UIView beginAnimations:@“拖动可拖动视图”上下文:nil];
////////////////////////
//如果(location.x>50&&location.x,这是我在ios中拖动superview视图的代码。
假设我在子视图上有一个按钮。当这个按钮点击这个动作时,它会检查子视图是否放置在x==0,它会将它设置为x=260,反之亦然
注意,还必须在action.setAnimationDuration的.m文件中添加框架QuartzCore/QuartzCore和#导入QuartzCore/QuartzCore.h显示拖动的持续时间
-(iAction)列表按钮已添加{
// [self.navigationController popViewControllerAnimated:YES];
//[self.profileTableView reloadData];
NSLog(@"ButtonTapped");
if(profileView.frame.origin.x == 0){
[UIView beginAnimations:@"advancedAnimations" context:nil];
[UIView setAnimationDuration:3.0];
CGRect aFrame = profileView.frame;
aFrame.origin.x = 260;
profileView.frame = aFrame;
[UIView commitAnimations];
}
else{
[UIView beginAnimations:@"advancedAnimations" context:nil];
[UIView setAnimationDuration:3.0];
CGRect aFrame = profileView.frame;
aFrame.origin.x = 0;
profileView.frame = aFrame;
[UIView commitAnimations];
}
}
这里按下按钮,视图正朝着增加x位置的方向拖动
这里图像结束拖动过程
现在,若再次按下白色子视图左侧上方的按钮,它将再次出现在x=0位置。
我希望它能帮助您!但是为什么要使用-(void)touchsmoved:(NSSet*)touchs with event:(UIEvent*)event{}。此方法用于从图像获取触摸事件。从自定义类选项卡将视图更改为UIcontroll,然后它将获取触摸事件。