我有一个包含自定义UIView
的{{1}}类。当此UISlider
添加到viewController时,它将使用
UIView
现在我要做的是为滑块末端飞出的newSlider.transform = CGAffineTransformRotate(newSlider.transform, degreesToRadians(random));
拇指图像设置动画。
我面临的问题是获取滑块开始/结束的坐标,以便我可以计算出拇指图像的行进方式。
我尝试使用UISlider
,但无论应用于trackRectForBounds
我在UIView
课程中尝试了这些:
UIView
和
CGRect trackRect = [customSlider trackRectForBounds:customSlider.bounds];
给我{{2,1},{288,50}}& {{2,215},{316,50}}无论轮换如何。我认为它是从CGRect trackRect2 = [customSlider trackRectForBounds:self.window.bounds];
而不是屏幕给我的矩形。
答案 0 :(得分:1)
获取滑块的终点可以这样做:
CGFloat rotation = [[newSlider.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
CGFloat halfWidth = newSlider.bounds.size.width/2;
CGPoint sliderCenter = newSlider.center;
sliderCenter.y += newSlider.bounds.size.height/2; //this shouldn't be needed but it seems it is
CGFloat x1 = sliderCenter.x - halfWidth * cos(rotation);
CGFloat x2 = sliderCenter.x + halfWidth * cos(rotation);
CGFloat y1 = sliderCenter.y - halfWidth * sin(rotation);
CGFloat y2 = sliderCenter.y + halfWidth * sin(rotation);
CGPoint T1 = CGPointMake(x1, y1);
CGPoint T2 = CGPointMake(x2, y2);