簡體   English   中英

Touchesbegan和touchesmoved函數無法在UIImageView上運行

[英]Touchesbegan and touchesmoved functions not working on UIImageView

我在我的FYImageSequence.m文件中有這兩個函數。我從UIImageView繼承了這個類

@interface FVImageSequence : UIImageView

我能夠在故事板中將它連接到我的UIImageView。但是如何使下面的功能工作。是否有任何連接應該完成。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(increment == 0)
    increment = 1;
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    previous = touchLocation.x;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];
int location = touchLocation.x;

if(location < previous)
    current += increment;
else
    current -= increment;

previous = location;

if(current > numberOfImages)
    current = 0;
if(current < 0)
    current = numberOfImages;

NSString *path = [NSString stringWithFormat:@"%@%d", prefix, current];
NSLog(@"%@", path);

path = [[NSBundle mainBundle] pathForResource:path ofType:extension];


UIImage *img =  [[UIImage alloc] initWithContentsOfFile:path];

[self setImage:img];
}

UIImageView用戶交互默認為NO 所以你需要這樣做

self.userInteractionEnabled = YES;

在圖像視圖上將UserInteractionEnabled設置為YES

為此,我不得不使用該方法,而不是變量(不知何故變量不能直接訪問),所以:
[self setUserInteractionEnabled:YES];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM