如何在改变UIImageView时制作动画

发布于 2024-11-09 09:07:45 字数 1030 浏览 2 评论 0原文

我编写了这段代码并制作了 2 个按钮(下一个和后退),以使用 UIImageView 在 NSMutubleArray 中显示图片。

-(IBAction) pictureNext:(id)sender;
{
    if (pictureIndice==[promMUAraay count]) 
    {
        pictureIndice=0;
    }

    NSLog(@"indice : %d ",pictureIndice);
    Promotion *prom = [[Promotion alloc]init];
    prom =[promMUAraay objectAtIndex:pictureIndice];
    promotionPicture.image = [UIImage imageWithData:prom.pPicture];
    //promLabel.text=prom.pName;
    NSLog(@"label : %@",prom.pName);
    pictureIndice++;
    }

-(IBAction) pictureBack:(id)sender;
{
    if (pictureIndice==[promMUAraay count]) 
    {
        pictureIndice=0;
    }

    NSLog(@"indice : %d ",pictureIndice);
    Promotion *prom = [[Promotion alloc]init];
    prom =[promMUAraay objectAtIndex:pictureIndice];
    promotionPicture.image = [UIImage imageWithData:prom.pPicture];
    //promLabel.text=prom.pName;
    if (pictureIndice==0) 
    {
        pictureIndice=[promMUAraay count];
    }
    pictureIndice--;


}

这可行,但是可以在更改图像时制作一些动画吗?

I wrote this code and made 2 buttons, next and back, to display a picture in an NSMutubleArray using an UIImageView.

-(IBAction) pictureNext:(id)sender;
{
    if (pictureIndice==[promMUAraay count]) 
    {
        pictureIndice=0;
    }

    NSLog(@"indice : %d ",pictureIndice);
    Promotion *prom = [[Promotion alloc]init];
    prom =[promMUAraay objectAtIndex:pictureIndice];
    promotionPicture.image = [UIImage imageWithData:prom.pPicture];
    //promLabel.text=prom.pName;
    NSLog(@"label : %@",prom.pName);
    pictureIndice++;
    }

-(IBAction) pictureBack:(id)sender;
{
    if (pictureIndice==[promMUAraay count]) 
    {
        pictureIndice=0;
    }

    NSLog(@"indice : %d ",pictureIndice);
    Promotion *prom = [[Promotion alloc]init];
    prom =[promMUAraay objectAtIndex:pictureIndice];
    promotionPicture.image = [UIImage imageWithData:prom.pPicture];
    //promLabel.text=prom.pName;
    if (pictureIndice==0) 
    {
        pictureIndice=[promMUAraay count];
    }
    pictureIndice--;


}

This works, but is it possible to make a little animation when changing image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-11-16 09:07:46

有很多动画选项可供选择。由于您有一个 UIImageView 对象,因此这里是一个“淡出/淡入”示例(警告:未测试):

-(void)fadeView:(UIView *)thisView fadeOut:(BOOL)fadeOut {
    //   self.ReviewViewController.view.alpha = 1;

    [UIView beginAnimations:@"fadeOut" context:nil]; 
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];

    if (fadeOut) {
        thisView.alpha = 0;
    } else {
        thisView.alpha = 1;
    }
    [UIView commitAnimations];
}

然后在您的 pictureNextpictureBack 您可以将 line: 更改

   promotionPicture.image = [UIImage imageWithData:prom.pPicture];

为以下 3 行:

   [self fadeView:promotionPicture fadeOut:YES];
   promotionPicture.image = [UIImage imageWithData:prom.pPicture];
   [self fadeView:promotionPicture fadeOut:NO];

旁注:

您可以通过让两个按钮调用相同的选择器并确定您的sender是后退还是前进按钮。

-(IBAction)pictureMove:(id)sender {
    if (sender == yourNextButton) {
        //do next index math here
    } else if (sender == yourBackButton) {
        //do back index math here
    }
    .
    .
    .
    //you image change code goes here
    .
    .
    .
}

There are a lot of animation options to choose from. Since you have a single UIImageView object, here is a "fade out/in" example (warning: not tested):

-(void)fadeView:(UIView *)thisView fadeOut:(BOOL)fadeOut {
    //   self.ReviewViewController.view.alpha = 1;

    [UIView beginAnimations:@"fadeOut" context:nil]; 
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];

    if (fadeOut) {
        thisView.alpha = 0;
    } else {
        thisView.alpha = 1;
    }
    [UIView commitAnimations];
}

Then in your pictureNext and pictureBack you can change the line:

   promotionPicture.image = [UIImage imageWithData:prom.pPicture];

to these 3 lines:

   [self fadeView:promotionPicture fadeOut:YES];
   promotionPicture.image = [UIImage imageWithData:prom.pPicture];
   [self fadeView:promotionPicture fadeOut:NO];

Side note:

You could combine your pictureNext and pictureBack by having both buttons call the same selector and determining if you sender is the back or forward button.

-(IBAction)pictureMove:(id)sender {
    if (sender == yourNextButton) {
        //do next index math here
    } else if (sender == yourBackButton) {
        //do back index math here
    }
    .
    .
    .
    //you image change code goes here
    .
    .
    .
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文