Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从UIImageview的子类调用方法_Iphone_Class_Methods_Touch_Subclass - Fatal编程技术网

Iphone 从UIImageview的子类调用方法

Iphone 从UIImageview的子类调用方法,iphone,class,methods,touch,subclass,Iphone,Class,Methods,Touch,Subclass,我有两个.m文件。第一个是主代码,第二个是UIImageView的子类,因此我可以检测触摸。 在main.m文件中,我添加了一个进度条和一个customimageview,它们都是scrollview的子视图 我需要的是,当用户触摸customimageview时,进度条会向上移动,双击会降低[注意:customimageview必须在秒.m中识别其触摸,因为它们位于scrollview的子视图中,并且必须处理其他控件] 在main.m文件中,我有两种方法: - (void)pumpsingle

我有两个.m文件。第一个是主代码,第二个是UIImageView的子类,因此我可以检测触摸。 在main.m文件中,我添加了一个进度条和一个customimageview,它们都是scrollview的子视图

我需要的是,当用户触摸customimageview时,进度条会向上移动,双击会降低[注意:customimageview必须在秒.m中识别其触摸,因为它们位于scrollview的子视图中,并且必须处理其他控件]

在main.m文件中,我有两种方法:

- (void)pumpsingletap {  
   progbm.progress +=0.1;  
}  

- (void)pumpdoubletap {  
   progbm.progress -=0.1;  
}  
在子类uiimageview中,我有:

//inside touches method
if ([touch view].tag == 555) {  
   NSLog(@"pump touched");  
   switch ([allTouches count]) {  
         case 1: {  
                switch ([touch tapCount]) {  
                     //---single tap---  
                     case 1: {  
                     NSLog(@"single pump touch");  
                     [self performSelector:@selector(pumpsingletap) withObject:nil afterDelay:.4];  
                     } break;  
                     //---double tap---  
                     case 2: {  
                     NSLog(@"double pump touch");  
                     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(pumpsingletap) object:nil];  
                     [self performSelector:@selector(pumpdoubletap) withObject:nil afterDelay:.4];  
                     } break;  
               }  
           }  
       }  
  }
因此,NSlog的出现使得触摸识别不成问题。但是performSelector失败了。因为customimageview pumpsingletap不工作

那么如何在子类中调用该方法呢

//更新//

所以我在下面的代码中,在我的子类中添加了

mainMethod* callingMethod = [[mainMethod alloc] init];  
[callingMethod performSelector:@selector(pumpsingletap) withObject:nil afterDelay:.4];  
然后,在pumpsingletap的主要方法中,我将其更改为:

- (void)pumpsingletap { 
   NSLog(@"single pump method called");
   progbm.progress +=0.1;  
} 

调用的单泵方法的NSLog出现,但进度条progbm-未移动。所以我已经解决了我的呼叫问题-现在只需要找出为什么进度条不移动

如果我正确地遵循了代码,那么您是在self上执行选择器,但是self是您的派生UIImageView类,因此您可能会在这一点上崩溃?。您需要对主文件中的类执行选择器。将对该类的引用传递给派生类。或者,您可以创建一个委托,在主类中实现它,将主类传递给UIImageView,然后通过委托调用。甚至还有其他方法可以进行键值观察,但其中之一应该可以工作。

我不确定这是否是问题所在,但您不需要在case语句周围加括号。我也不明白为什么你会在一个开关中做一个开关。只要做一个if-elseif-else语句,它可能会更容易理解

除此之外,据我所知,您有一个视图控制器,其中包含一个进度条和一个customimageview作为属性,并且您有一些方法应该被调用以响应某些操作,点击或双击customimageview,但它们在视图控制器中。解决这个问题的通常方法是使用目标操作机制。uicontrol通过封装目标操作对并将其存储在由事件类型uicontrol事件键入的字典中来实现目标操作机制。这里有一个稍微简单的版本

在UIImageView子类的.h文件中,在@interface之前,编写以下内容:

typedef enum {
     ControlEventTap = 0,
     ControlEventDoubleTap 
} ControlEvent;
然后在.m文件中,在@implementation之前添加以下内容:

@interface TargetActionPair : NSObject {
     id target;    
     SEL action;
} 

@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;

@end

@implementation TargetActionPair

@synthesize target, action;

@end
然后,向customimageview实现中添加NSMutableArray实例变量,但不添加属性和-voidsetTarget:idt action:SELa forEvent:ControlEvente方法

方法应如下所示:

- (void)setTarget:(id)t action:(SEL)a forEvent:(ControlEvent)e {
    TargetActionPair *tar_act = [[TargetActionPair alloc] init];
    tar_act.target = t;
    tar_act.action = a;
    // actionsArray is the mutable array instance variable and must be allocated and set in the init method for customimageview.
    [actionsArray replaceObjectAtIndex:(NSUInteger)e withObject:tar_act];
    [tar_act release];
}
[self.customimageview setTarget:self action:@selector(pumpsingletap) forEvent:ControlEventTap];
[self.customimageview setTarget:self action:@selector(pumpdoubletap) forEvent:ControlEventDoubleTap];
然后,您可以将触摸处理代码替换为:

if ([touch view].tag == 555) {
    NSUInteger tapcount = [touch tapCount];
    if (([alltouches count] == 1) && (tapcount <= [actionsArray count])) {
        TargetActionPair *tar_act = [actionsArray objectAtIndex:tapcount-1];
        [tar_act.target performSelector:tar_act.action withObject:nil afterDelay:.4];
        if (tapcount == 2) {
            TargetActionPair *tar_act2 = [actionsArray objectAtIndex:tapcount-2];
            [NSObject cancelPreviousPerformRequestsWithTarget:tar_act2.target selector:tar_act2.action object:nil];
        }
    }
}
不要忘记在dealloc方法中释放actionsArray,并且在释放视图控制器时要非常小心,因为customimageview没有保留它


我希望这有帮助,祝你的应用程序好运。

最后,我通过使用NSNotificationCenter解决了这个问题。

调用主文件类选择器的代码行是什么。请确保在interface builder中连接了进度条