iPhone垂直拨动开关

时间:2010-05-14 18:03:59

标签: iphone iphone-sdk-3.0

我正在尝试为iPhone创建一个垂直切换开关控件(沿着UISwitch但垂直滑动)。

我想知道现有的控件是否已经存在,或者是否有任何好的教程可以解释为iPhone创建自定义控件的基础知识。

目前我已尝试使用仿射变换从基本UI切换创建垂直切换,除了滑块与滑块轨道相比太小外,它的工作原理,因此我正在寻找有关编写自定义控件的信息。 / p>

非常感谢任何方向。

3 个答案:

答案 0 :(得分:1)

您可以使用UIButton和两个图像使其看起来像垂直切换开关,并根据开关状态交换图像。

子类UIButton,添加开关状态,图像交换等,根据需要使用。

编辑 - 另一种方法是完全自定义控件。你继承UIControl并添加类似于UISwitch函数的函数(即:initWithFrame,on,setOn:animated :)。

您还需要发送状态更改的事件,类似于UISwitch所做的事情:

[self sendActionsForControlEvents: UIControlEventValueChanged];

当触摸在交换机上移动时,您可以实现beginTrackingWithTouch,continueTrackingWithTouch和endTrackingWithTouch来滑动切换图像。我这样做是为了创建一个2D滑块。 UISwitch也有自己的本地化,例如ON / OFF变为0/1。

答案 1 :(得分:0)

在UISlider的特定情况下,您可以从中派生并覆盖一些选择方法:

// lets a subclass lay out the track and thumb as needed
- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds;
- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
- (CGRect)trackRectForBounds:(CGRect)bounds;
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;

默认实现基本上根据最后一次调用中的值水平滑动拇指。如果垂直滑动,则会有一个垂直滑块。只需将所有图像设置为您自己的自定义图稿,并实现这些方法以根据需要定位。你的thumbRect方法将最终得到一个如下所示的行:

result.origin.y = trackFrame.origin.y + ( trackFrame.size.height - thumbFrame.size.height ) * value;

答案 2 :(得分:0)

我使用了自定义滑块并设置了变换。在选择器中,我有一个简单的if语句,如果大于50则将其捕捉到100,如果更少则捕捉到0。我在网上找到了自定义幻灯片代码:http://www.iphonedevsdk.com/forum/iphone-sdk-development/11470-there-way-i-can-use-slide-unlock-slider.html

- (void)create_Custom_UISlider
{
CGRect frame = CGRectMake(100.0, 110.0, 180, 1.0    );
    CGRect thumb = CGRectMake(100.0, 110.0, 1.0, 1.0);
    customSlider = [[UISlider alloc] initWithFrame:frame];
    [customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    // in case the parent view draws with a custom color or gradient, use a transparent color
    customSlider.backgroundColor = [UIColor clearColor];
    customSlider.opaque = 20.0;
    UIImage *stetchLeftTrack = [[UIImage imageNamed:@"image3.png"]
                                stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
    UIImage *stetchRightTrack = [[UIImage imageNamed:@"image2.png"]
                                 stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
    [customSlider setThumbImage:    [UIImageimageNamed:@"image1.png"]forState:UIControlStateNormal];
    [customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
    [customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
    [customSlider thumbRectForBounds: thumb trackRect: frame value: customSlider.value];
    customSlider.minimumValue = 0.0;
    customSlider.maximumValue = 100.0;
    customSlider.continuous = NO;
    customSlider.value = 50.0;
    customSlider.transform = CGAffineTransformRotate(customSlider.transform,270.0/180*M_PI);
}

-(void)sliderAction:(id)sender 
{
    UISlider *slider = (UISlider *)sender;
    int progressAsInt = (int)(slider.value + 0.5f);
    customSliderValue = [NSNumber numberWithInt:0];

    customSliderValue = progressAsInt;

    if (customSliderValue > 50) {
        [self myMethod1];
        [customSlider setValue:100];

    }
    else {
        [self myMethod2];
        [customSlider setValue:0];
    }



}