Changing a UIImageView image with a slider
I have a view with one UIImageView and a slider. Slider Min 1 Max 10. When slider is moved UIImageView should display a different image. Slider is 1 test1.png will display, slider is 2 test2.png will display, slider is 3 test3.png will display etc...
In Interface Builder I have connected IBOutlet slider to Horizontal slider, IBOutlet image to Image View, & IBAction changeImage to Horizontal slider "Value Changed"
I've cast slider float to int. Placed an NSLog statement in IBAction changeImage to make sure method is being called, It is. I receive no error messages on build. However, when i build & Run and move slider in simulator image in UIImageView does not change. The problem must be "logic" I've hit a wall and can not figure it out.
#import <UIKit/UIKit.h>
@interface SliderViewController : UIViewController {
IBOutlet UISlider *slider;
IBOutlet UIImageView *image;
}
@property (nonatomic, retain) IBOutlet UISlider *slider;
@property (nonatomic, retain) IBOutlet UIImageView *image;
- (IBAction)changeImage;
@end
#import "SliderViewController.h"
@implementation SliderViewController
@synthesize slider;
@synthesize image;
- (IBAction)changeImage {
NSLog(@"changeImage method being called!!!!");
int intSlider = (int)(slider.value);
if (intSlider == 0) {
image.image = [UIImage imageNamed:@"ash1.png"];
}
if (intSlider == 1) {
image.image = [UIImage imageNamed:@"ash2.png"];
}
if (intSlider == 2) {
image.image = [UIImage imageNamed:@"ash3.png"];
}
开发者_开发百科 if (intSlider == 3) {
image.image = [UIImage imageNamed:@"ash4.png"];
}
if (intSlider == 4) {
image.image = [UIImage imageNamed:@"ash5.png"];
}
if (intSlider == 5) {
image.image = [UIImage imageNamed:@"ash6.png"];
}
if (intSlider == 6) {
image.image = [UIImage imageNamed:@"ash7.png"];
}
if (intSlider == 7) {
image.image = [UIImage imageNamed:@"ash8.png"];
}
if (intSlider == 8) {
image.image = [UIImage imageNamed:@"ash9.png"];
}
if (intSlider == 9) {
image.image = [UIImage imageNamed:@"ash10.png"];
}
if (intSlider == 10) {
image.image = [UIImage imageNamed:@"ash11.png"];
}
}
Make sure the image names are exactly right and that you've actually Added them to the project (not just copied them using Finder).
By the way, a switch-case
statement would be better than a series of if-then
statements here.
Another minor thing is that the if (intSlider == 0)
case will never happen since you're setting the minimum to 1.
According to the forums one method would be to try calling setNeedsDisplay to refresh the view:
myApp = (MyApp *)[[UIApplication sharedApplication] delegate];
[myApp.statView setNeedsDisplay];
I'm sure you could just call this on 'self' from your view.
[self.view setNeedsDisplay];
According to this post the call has to be in the main thread of the app as well. Not sure this will work, but worth a try.
changing image using valued slider in objective c
.h
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageview;
@property (weak, nonatomic) IBOutlet UISlider *sldrimg;
- (IBAction)slider:(id)sender;
.m
- (IBAction)slider:(id)sender {
int a = (int) self.sldrimg.value;
if(a<=20)
{
_imageview.image = [UIImage imageNamed:@"nature.png"];
}
if(a>20 && a<=40)
{
_imageview.image = [UIImage imageNamed:@"bike.png"];
}
if(a>40 && a<=60)
{
_imageview.image = [UIImage imageNamed:@"minion.png"];
}
if(a>60 && a<=80)
{
_imageview.image = [UIImage imageNamed:@"woods.png"];
}
if(a>80 && a<=100)
{
_imageview.image = [UIImage imageNamed:@"trees.png"];
}
精彩评论