iOS:在点击手势上更改背景图像

时间:2015-08-31 01:37:02

标签: ios objective-c

我设置了点击手势,当我在模拟器中点击它时,NSLog会触发它,所以我确定它已经点击了它:

@implementation CoolController

- (void)viewDidLoad {
    [super viewDidLoad];
    ...

    UITapGestureRecognizer *backgroundTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped)];
    backgroundTapRecognizer.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:backgroundTapRecognizer];
}

- (void) backgroundTapped {
    NSLog(@"cool"); // <-- this fires when I tap it twice
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table-2"]]];
}

但是,背景中的图像不会改变。我是否需要调用另一种方法来更新UI?

2 个答案:

答案 0 :(得分:0)

试试这个

  • (void)backgroundTapped:(id)发件人

答案 1 :(得分:0)

在viewDidLoad中尝试此操作并检查颜色是否发生变化:

  

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@&#34; table-2.png&#34;]]];

为您的图片添加扩展程序png / jpg无论它是什么。希望它有所帮助