如何在按钮操作上显示图像

时间:2014-03-06 08:30:36

标签: ios objective-c uiscrollview uibutton uiimage

我有10 UIImages。当我点击UIButton UIButton UIScrollView. But I need to implement the UIButton like **Next**, if UIImage`下显示的is clicked first time, it displays the first时,点击它后会显示下一张图片。如果单击后退按钮,则应显示上一张图像。

enter image description here

int   currentIndex = 0;
int MAX_COUNT;

NSMutableArray *imageName = [[NSMutableArray alloc] initWithObjects:
                                @"spices.jpg",
                                @"spice_powder.jpg", 
                                @"turmeric.jpg",
                                @"whynani_img3.jpg",
                                @"spice_blends.jpg",
                                @"products1.png", nil];

currentIndex = currentIndex + 1;
if(currentIndex > MAX_COUNT) {
   currentIndex = MAX_COUNT;
}        

for (int i = 0; i<[imageName count]; i++ ) {

   UIImageView  *mmageView = 
     [[UIImageView alloc] initWithFrame:CGRectMake(200,200,350,350)];
   [mmageView setImage:[UIImage imageNamed:[imageName objectAtIndex:i]]];            
   [self.view addSubview:mmageView];           
}

3 个答案:

答案 0 :(得分:1)

您可以使用具有2个UIButton的UIViewController执行相同操作,其中自定义图像与UIImageView一起向前和向后显示。在选择下一个/后退按钮时加载适当的图像。图像名称必须存储在NSArray中。

希望这有帮助。

编辑:示例代码,如下所示,

在.h文件中

@property(nonatomic, weak) UIImageView *imageView;
@property(nonatomic, weak) UIButton *nextButton;
@property(nonatomic, weak) UIButton *backButton;

(IBAction)btnNextBtn:(id)sender;
(IBAction)btnBackBtn:(id)sender;

在.m文件中

(void)viewDidLoad {
    currentIndex = 0;
    NSMutableArray *imageName = [[NSMutableArray alloc] initWithObjects:@"Myprofile.png", 
                                 @"great_ideas_wordle2.png", @"Review.png", 
                                 @"collaborate.png", nil];

   imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200,200,350,350)];
   [imageView setImage:[UIImage imageNamed:[imageName objectAtIndex:currentIndex]]];
   [self.view addSubView:imageView];
}

(IBAction)btnNextBtn:(id)sender {
     currentIndex = currentIndex + 1;
     if(currentIndex > MAX_COUNT) {
         currentIndex = MAX_COUNT;
     }
     // load the image view with the currentIndex from the array.
     [imageView setImage:[UIImage imageNamed:[imageName objectAtIndex:currentIndex]]];
}

(IBAction)btnBackBtn:(id)sender {
     currentIndex = currentIndex - 1;
     if (currentIndex < 0) {
         currentIndex = 0;
     }
     // load the image view with the currentIndex from the array.
     [imageView setImage:[UIImage imageNamed:[imageName objectAtIndex:currentIndex]]];
}

答案 1 :(得分:1)

在接口文件中声明全局 imageView int

UIImageView *imgView;
int index;

viewDidLoad:

index = 0;

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(200,200,350,350)];
[imgView setImage:[UIImage imageNamed:[imageName objectAtIndex:index]]];
[self.view addSubView:imgView]; 

Next Button Action

-(void)nextButtonAction
{
  ++index;

  [previousBtn setEnabled:YES]; 

  if (index > [imageName count])
  {
    index = [imageName count] - 1;
    [nextBtn setEnabled:NO];
  }

  [imgView setImage:[UIImage imageNamed:[imageName objectAtIndex:index]]];

}

Previous Button Action

-(void)previousButtonAction
{
  --index;

  [nextBtn setEnabled:YES]; 

  if (index < 0)
  {
    index = 0;
    [previousBtn setEnabled:NO];
  }

  [imgView setImage:[UIImage imageNamed:[imageName objectAtIndex:index]]];

}

答案 2 :(得分:0)

尝试使用此代码

.h文件

@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, strong) UIButton *nextButton;
@property(nonatomic, strong) UIButton *previousButton;
@property(nonatomic, strong) NSMutableArray *imagesArrayName;
@property(nonatomic) int   currentIndex;

-(IBAction)btnNextImage:(id)sender;
-(IBAction)btnPreviousImage:(id)sender;

.m文件

- (void)viewDidLoad {
    [super viewDidLoad];

    self.currentIndex = 0;

    NSMutableArray *imagesArrayName = [[NSMutableArray alloc] initWithObjects:
                                      @"spices.jpg",
                                      @"spice_powder.jpg", 
                                      @"turmeric.jpg",
                                      @"whynani_img3.jpg",
                                      @"spice_blends.jpg",
                                      @"products1.png", nil];

    [self.nextButton addTarget:self action:@selector(btnNextImage:) forControlEvents:UIControlEventTouchUpInside];
    [self.previousButton addTarget:self action:@selector(btnPreviousImage:) forControlEvents:UIControlEventTouchUpInside];

    [self.imageView setImage[UIImage imageNamed:(NSString*)[imagesArrayName objectAtIndex:currenIndex]]];

    [self.previousButton setEnabled:FALSE];

}

-(IBAction)btnNextImage:(id)sender {

    self.currentIndex = self.currentIndex + 1;

    [self.imageView setImage[UIImage imageNamed:(NSString*)[imagesArrayName objectAtIndex:currenIndex]]];

    if (self.currentIndex == [self.imagesArrayName count] - 1) {

        [self.nextButton setEnabled:FALSE];
    }
    [self.previousButton setEnabled:TRUE];

}
-(IBAction)btnPreviousImage:(id)sender{

    self.currentIndex = self.currentIndex - 1;

    [self.imageView setImage[UIImage imageNamed:(NSString*)[imagesArrayName objectAtIndex:currenIndex]]];

    if (self.currentIndex == 0) {

        [self.previousButton setEnabled:FALSE];
    }
    [self.nextButton setEnabled:TRUE];
}