gpt4 book ai didi

ios - 如何在使用 objective-c 代码在 ios 中设置动画时识别 UIImageView 上的触摸事件?

转载 作者:行者123 更新时间:2023-11-29 12:28:09 25 4
gpt4 key购买 nike

您好,我正在通过以下代码在 UIImageView 中为 4 个图像设置动画。但问题是图像没有出现在 UIImageView 中。如果我尝试删除 swipeImage 代码,则图像出现在 UIImageView 中。如何让 imageview 出现在 UIImageView 中?

Oulet 已经对 UIImageView 属性进行了设置,如果我尝试在没有动画代码的情况下设置图像,UIImageView 有图像。使用动画,UIImageView 没有图像的外观。在此先感谢您的帮助。

属性

@property (weak , nonatomic )IBOutlet UIImageView *imgslide;

声明

    int scrlNo;
NSTimer *aTimer;

查看加载代码

  scrlNo = 0;

imgslide.userInteractionEnabled = YES;
[self addGestures];
[self SwipeImages];

添加手势代码

-(void)addGestures //Adding Single Tap Gesture here
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTap:)]; //init the TapGesture with the selector
singleTap.cancelsTouchesInView = YES;
[imgslide addGestureRecognizer:singleTap]; //Add it to imgslide, not to self.view

}

ImageViewTap代码

-(void)imageViewTap :(UITapGestureRecognizer *) gesture
{
NSLog(@"Clicked image %d", scrlNo);
}

滑动图片代码或动画代码

- (void)SwipeImages
{
if (scrlNo==1) {

imgslide.image = [UIImage imageNamed:@"promo_banner_2.png"];


scrlNo=2;
[aTimer invalidate];

aTimer = nil;
aTimer=[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(SwipeImages)
userInfo:nil
repeats:YES];

}else if (scrlNo==0) {



// UIImage * img = [UIImage imageNamed:@"promo_banner_1.png"];
//
// [self.imageViewOutlet setImage:img];

imgslide.image = [UIImage imageNamed:@"promo_banner_1.png"];


scrlNo=1;
[aTimer invalidate];

aTimer = nil;
aTimer=[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(SwipeImages)
userInfo:nil
repeats:YES];

}
else if (scrlNo==2) {

// UIImage * img = [UIImage imageNamed:@"promo_banner_3.png"];
//
// [self.imageViewOutlet setImage:img];

imgslide.image = [UIImage imageNamed:@"promo_banner_3.png"];


scrlNo=3;
[aTimer invalidate];

aTimer = nil;
aTimer=[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(SwipeImages)
userInfo:nil
repeats:YES];

}
else if (scrlNo==3) {


//
// UIImage * img = [UIImage imageNamed:@"promo_banner_7.png"];
//
// [self.imageViewOutlet setImage:img];

imgslide.image = [UIImage imageNamed:@"promo_banner_7.png"];


scrlNo=0;
[aTimer invalidate];

aTimer = nil;
aTimer=[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(SwipeImages)
userInfo:nil
repeats:YES];
}
else if (scrlNo==4) {

}
}

最佳答案

添加此方法并在 viewDidLoad 上加载此方法,而不是在 viewDidLoad 上加载 SwipeImages

-(void)addTimer
{
aTimer=[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(SwipeImages)
userInfo:nil
repeats:YES];
}

然后你的 viewDidLoad 看起来像

scrlNo = 0;

imgslide.userInteractionEnabled = YES;
[self addGestures];
[self addTimer];

并为此编辑 SwipeImages,

- (void)SwipeImages
{
if (scrlNo==1) {
imgslide.image = [UIImage imageNamed:@"promo_banner_2.png"];
scrlNo=2;

}else if (scrlNo==0) {
imgslide.image = [UIImage imageNamed:@"promo_banner_1.png"];
scrlNo=1;
}
else if (scrlNo==2) {
imgslide.image = [UIImage imageNamed:@"promo_banner_3.png"];
scrlNo=3;
}
else if (scrlNo==3) {
imgslide.image = [UIImage imageNamed:@"promo_banner_7.png"];
scrlNo=0;
}
else if (scrlNo==4) {

}
}

关于ios - 如何在使用 objective-c 代码在 ios 中设置动画时识别 UIImageView 上的触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com