我正在尝试按照UISlider将两个UIImageView移动到一起。这是一张更好理解的图片:
当滑块的球滑到最左侧(值为零)时,只显示绿色。当它滑到最右边(值为600)时,只显示红色。如果滑块的值为480,则应显示红色图片的80%和绿色的20%。我用来尝试实现这个的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.slider.minimumValue = 0;
self.slider.maximumValue = 600;
}
- (IBAction)sliderChanged:(id)sender {
[imageViewLeft setFrame:CGRectMake(slider.value - 50, imageViewLeft.frame.origin.y,imageViewLeft.frame.size.width,imageViewLeft.frame.size.height)];
[imageViewRight setFrame:CGRectMake(slider.value + 50, imageViewRight.frame.origin.y,imageViewRight.frame.size.width,imageViewRight.frame.size.height)];
}
这是正常工作,但第二次更改滑块,红色图像(名为imageViewLeft)缩小,而imageViewRight保持相同的大小。我不能为我的生活弄清楚为什么(我仍然非常新的编程和iOS一般)。有谁知道我应该改变什么或如何最好地完成这个?
答案 0 :(得分:2)
通过将绿色背景保持为全屏并仅调整红色视图,您可以实现更多或更少相同的功能:
- (void)viewDidLoad
{
[super viewDidLoad];
self.slider.minimumValue = 0;
self.slider.maximumValue = 600;
imageViewRight.frame = self.view.bounds;
}
- (IBAction)sliderChanged:(id)sender {
[imageViewLeft setFrame:CGRectMake(0, 0,(self.slider.value/self.slider.maximumValue)*sliderimageViewRight.frame.size.width,imageViewRight.frame.size.height)];
}
或者,如果你仍然希望边缘到边缘都有两种观点,那么你可以这样做:
- (void)viewDidLoad
{
[super viewDidLoad];
self.slider.minimumValue = 0;
self.slider.maximumValue = 600;
}
- (IBAction)sliderChanged:(id)sender {
[imageViewLeft setFrame:CGRectMake(0, 0,(self.slider.value/self.slider.maximumValue)*self.view.bounds.size.width,self.view.bounds.size.height)];
[imageViewRight setFrame:CGRectMake(imageViewLeft.frame.size.width, 0,self.view.bounds.size.width-imageViewLeft.frame.size.width,self.view.bounds.size.height)];
}
答案 1 :(得分:1)
[self.red setFrame:CGRectMake(slider.value+50-self.view.frame.size.width, imageViewLeft.frame.origin.y,self.view.frame.size.width, imageViewLeft.frame.size.height)];
[self.green setFrame:CGRectMake(slider.value+50, imageViewRight.frame.origin.y,self.view.frame.size.width, imageViewRight.frame.size.height)];
并将最大滑块值设置为其宽度:
self.slider.maximumValue = self.slider.frame.size.width;