Iphone UipanGesture未在Uiswitch上运行
我添加了一个UI开关,希望通过手势将其移动到另一个位置。但是uipangesture并没有在uiswitch上工作。。下面是我的代码Iphone UipanGesture未在Uiswitch上运行,iphone,ios5,uiswitch,uipangesturerecognizer,Iphone,Ios5,Uiswitch,Uipangesturerecognizer,我添加了一个UI开关,希望通过手势将其移动到另一个位置。但是uipangesture并没有在uiswitch上工作。。下面是我的代码 UISwitch *swich = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 40, 50)]; swich.userInteractionEnabled = YES; UIPanGestureRecognizer *gesture = [[UIPanGestureRecogni
UISwitch *swich = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 40, 50)];
swich.userInteractionEnabled = YES;
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(switchdraged:)];
[swich addGestureRecognizer:gesture];
[self.view addSubview:swich];
未调用switchdraged函数
下面是开关拖动功能
- (void)switchdraged:(UIPanGestureRecognizer *) gesture1
{
UISwitch *swich = (UISwitch *)gesture1.view;
CGPoint translation = [gesture1 translationInView:swich];
swich.center = CGPointMake(swich.center.x + translation.x,
swich.center.y + translation.y);
// reset translation
[gesture1 setTranslation:CGPointZero inView:swich];
}
我认为
uipangestrerecognizer
不能与UISwitch
一起工作
课堂参考说
UIPangestureRecognitor是UIgestureRecognitor的一个具体子类
它查找平移(拖动)手势。用户必须正在按
平移视图时,一个或多个手指在视图上
因此,UISwitch可能会被覆盖以更改状态
我已经创建了一个示例应用程序
相同的代码可以与UILabel
而不是UISwitch
配合使用。我和阿比泽恩讨论过。他告诉我,更改状态可能会覆盖它
请详细描述您需要如何使用UISwitch
这样可能对你有帮助
1.在视图内创建一个开关并添加为子视图
- (void)viewDidLoad
{
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
containerView.backgroundColor = [UIColor blackColor];
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(switchdraged:)];
[containerView addGestureRecognizer:gesture];
UISwitch *swich = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 40, 50)];
swich.userInteractionEnabled = YES;
[containerView addSubview:swich];
[self.view addSubview:containerView];
[self.view bringSubviewToFront:containerView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)switchdraged:(UIPanGestureRecognizer *) gesture1
{
NSLog(@"triggered");
UIView *swich = (UIView *)gesture1.view;
CGPoint translation = [gesture1 translationInView:swich];
swich.center = CGPointMake(swich.center.x + translation.x,
swich.center.y + translation.y);
// reset translation
[gesture1 setTranslation:CGPointZero inView:swich];
}
或
此链接可能对您有所帮助
我认为
uipangestrerecognizer
无法与UISwitch
一起工作
课堂参考说
UIPangestureRecognitor是UIgestureRecognitor的一个具体子类
它查找平移(拖动)手势。用户必须正在按
平移视图时,一个或多个手指在视图上
因此,UISwitch可能会被覆盖以更改状态
我已经创建了一个示例应用程序
相同的代码可以与UILabel
而不是UISwitch
配合使用。我和阿比泽恩讨论过。他告诉我,更改状态可能会覆盖它
请详细描述您需要如何使用UISwitch
这样可能对你有帮助
1.在视图内创建一个开关并添加为子视图
- (void)viewDidLoad
{
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
containerView.backgroundColor = [UIColor blackColor];
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(switchdraged:)];
[containerView addGestureRecognizer:gesture];
UISwitch *swich = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 40, 50)];
swich.userInteractionEnabled = YES;
[containerView addSubview:swich];
[self.view addSubview:containerView];
[self.view bringSubviewToFront:containerView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)switchdraged:(UIPanGestureRecognizer *) gesture1
{
NSLog(@"triggered");
UIView *swich = (UIView *)gesture1.view;
CGPoint translation = [gesture1 translationInView:swich];
swich.center = CGPointMake(swich.center.x + translation.x,
swich.center.y + translation.y);
// reset translation
[gesture1 setTranslation:CGPointZero inView:swich];
}
或
此链接可能对您有所帮助
我们可以看到LabelDraged的定义吗:?我已经添加了这个定义,请检查我个人从未使用过这个。。但是你可以看看这里。。希望此帮助扫描我们看到LabelDraged的定义:?我已经添加了定义,请检查我个人从未使用过此。。但是你可以看看这里。。希望这能有所帮助如果是这样的话,那么把开关放在UIView中,并在其中添加UIPanGesture应该会起作用。@AnkitSrivastava:我也添加了这一点:)嗨ramshad你能帮我解决这个问题吗?我将不胜感激是的。。我要把答案贴出来。。在此之前,如果我的解决方案有效,你可以通过勾选记号符号(显示我答案的左上角或向上投票的下方)来接受我的答案,如果是这样的话,那么将开关置于UIView中并向其添加UIPangEstata应该会起作用。@AnkitSrivastava:我也添加了这一点:)嗨ramshad你能帮我解决这个问题吗?我将不胜感激是的。。我要把答案贴出来。。在此之前,如果我的解决方案有效,您可以通过勾选勾号符号(显示我答案的左上方或上方选票下方)来接受我的答案