Iphone 如何创建在简单触摸中调用的各种按钮
我必须制作一个大约有20个按钮的应用程序,如果我从一个按钮切换到另一个按钮而不移除触摸(即通过拖动触摸),我想在进入该按钮的框架区域时调用该功能。Iphone 如何创建在简单触摸中调用的各种按钮,iphone,ios,xcode,uibutton,Iphone,Ios,Xcode,Uibutton,我必须制作一个大约有20个按钮的应用程序,如果我从一个按钮切换到另一个按钮而不移除触摸(即通过拖动触摸),我想在进入该按钮的框架区域时调用该功能。 例如:当我将触控从按钮的标签10拖动到按钮的标签11时,它应该调用按钮的标签11选择器方法。您应该用下面的方法注册事件 [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDragEnter]; 我想你不能用ui按钮来做这件事。
例如:
当我将触控从按钮的标签10拖动到按钮的标签11时,它应该调用按钮的标签11选择器方法。您应该用下面的方法注册事件
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDragEnter];
我想你不能用
ui按钮来做这件事。但是我有一个建议,也许对你有帮助。添加图像
而不是按钮
设置那些userIntractionEnable:NO
然后在touchesMoved
方法中,通过比较X,Y
坐标,您可以调用您的@选择器
方法
我为此写了一个小代码-
//I added your example image on my self.view with a view
//of 320x140 and set it userIntractionEnabled:NO
//Now in touchMoved: method did this..
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint pt = [touch locationInView:self.view];
//checking the touch is in self.view or not
if([touch view] == self.view)
{
//This is like a 2D array, So you have to follow row and column pattern.
if((pt.y>=0.0 && pt.y<=70.0))//For First Row
{
//These 7 are FirstRow Columns
if(pt.x>=0.0 && pt.x<=45.0){
NSLog(@"Method - 10");
}
if(pt.x>=46.0 && pt.x<=90.0){
NSLog(@"Method - 11");
}
if(pt.x>=91.0 && pt.x<=135.0){
NSLog(@"Method - 12");
}
if(pt.x>=136.0 && pt.x<=180.0){
NSLog(@"Method - 13");
}
if(pt.x>=181.0 && pt.x<=225.0){
NSLog(@"Method - 14");
}
if(pt.x>=226.0 && pt.x<=270.0){
NSLog(@"Method - 15");
}
if(pt.x>=271.0 && pt.x<=315.0){
NSLog(@"Method - 16");
}
}
//Row change this is for Second Row
if((pt.y>=71.0 && pt.y<=140.0))
{
//These 7 are SecondRow Columns
if(pt.x>=0.0 && pt.x<=45.0){
NSLog(@"Method - 17");
}
if(pt.x>=46.0 && pt.x<=90.0){
NSLog(@"Method - 18");
}
if(pt.x>=91.0 && pt.x<=135.0){
NSLog(@"Method - 19");
}
if(pt.x>=136.0 && pt.x<=180.0){
NSLog(@"Method - 20");
}
if(pt.x>=181.0 && pt.x<=225.0){
NSLog(@"Method - 21");
}
if(pt.x>=226.0 && pt.x<=270.0){
NSLog(@"Method - 22");
}
if(pt.x>=271.0 && pt.x<=315.0){
NSLog(@"Method - 23");
}
}
}
}
//我在我的self.view中添加了您的示例图像
//并将其设置为UserInActionEnabled:否
//现在在touchMoved:method中实现了这一点。。
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[[event AllTouchs]anyObject];
CGPoint pt=[触摸位置视图:self.view];
//检查触摸屏是否处于自检状态
如果([touch view]==self.view)
{
//这就像一个二维数组,所以您必须遵循行和列模式。
如果((pt.y>=0.0&&pt.y=0.0&&pt.x=46.0&&pt.x=91.0&&pt.x=136.0&&pt.x=181.0&&pt.x=226.0&&pt.x=271.0&&pt.x=71.0&&pt.y=0&&pt.x=46.0&&pt.x=91.0&&pt.0&&pt.x=136.0&&pt.x=181.0&&pt.x=226.0&&pt.x=271.0&&pt.x=271.0&&pt.x=271.0)您可以在自己的代码视图中对图像进行比较…并根据您的图像位置给出if条件下的方法。。。