-1

我有这个图像是 UIButton:

前

我想添加某种手势(平移手势)将其向右滑动,并且我希望图像向右拉伸,如下所示:

后

它与 iPhone 的“滑动解锁”方法非常相似,但没有从左到右移动的相同图像。

有可能这样做吗?

编辑:

我在按钮中添加了一个 UIPanGestureRecognizer,如下所示:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidDragged:)];
[self.buttonAbout addGestureRecognizer:panGesture];

self.buttonFrame = self.buttonAbout.frame;

我还将按钮框架保存到 CGRect 中。

现在这是 'buttonDidDragged' 方法,但我也有一个问题,按钮似乎向右移动,但图像保持不变。它不会伸展。

- (IBAction)buttonDidDragged:(UIPanGestureRecognizer*)gesture
{
    CGPoint translation = [gesture translationInView:self.view];

//    gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y);

    NSLog(@"%f",gesture.view.frame.origin.x + gesture.view.frame.size.width);
    gesture.view.frame = CGRectMake(gesture.view.frame.origin.x,
                                    gesture.view.frame.origin.y,
                                    gesture.view.frame.size.width + translation.x,
                                    gesture.view.frame.size.height);

    UIButton *button = (UIButton*)gesture.view;
    UIImage *buttonImage = [UIImage imageNamed:@"about_us_button.png"];
    buttonImage = [buttonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 80, 0, 0)];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:buttonImage forState:UIControlStateHighlighted];

    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.5
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             CGPoint finalPoint = CGPointMake(self.buttonFrame.origin.x + (self.buttonFrame.size.width / 2),
                                                              self.buttonFrame.origin.y + (self.buttonFrame.size.height / 2));
//                             gesture.view.center = finalPoint;
                             button.frame = newFrame; // This is where you change the frame of the button to make it stretch.
                         } completion:nil];
    }


    [gesture setTranslation:CGPointMake(0, 0) inView:self.view];
}
4

4 回答 4

1

好主意!是的,这是可能的。您可以拥有三张图像,左、右和中间,中间的一张只有一个像素宽。

您可以在 中调整图像touchesMoved。在那里,您可以根据触摸位置计算必要的拉伸宽度。

使用以下方法进行拉伸非常容易

[UIImage 可拉伸ImageWithLeftCapWidth:topCapHeight]

(UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets 
                          resizingMode:(UIImageResizingMode)resizingMode
于 2013-06-19T15:02:35.780 回答
0

最后,这就是我所做的,而且效果很好。非常感谢大家!

CGPoint translation = [gesture translationInView:self.view];
UIButton *button = (UIButton*)gesture.view;

if (translation.x < kTranslationMaximum && translation.x > kTranslationMinimum)
{
    self.aboutCenterImage.frame = CGRectMake(self.aboutCenterImage.frame.origin.x,
    self.aboutCenterImage.frame.origin.y,
    translation.x,
    self.aboutCenterImage.frame.size.height);

    gesture.view.frame = CGRectMake(self.aboutCenterImage.frame.origin.x + self.aboutCenterImage.frame.size.width,
    self.aboutCenterImage.frame.origin.y,
    self.buttonAbout.frame.size.width,
    self.aboutCenterImage.frame.size.height);
}
else if (translation.x > kTranslationMaximum)
{
    // Push ViewController or do whatever you like to do :)
}
于 2013-07-28T08:11:35.790 回答
0

这样做,在.h

CGSize dragViewSize;

然后,

-(IBAction)buttonDidDragged:(UIPanGestureRecognizer*)gesture
{
    CGPoint translation = [gesture translationInView:self.view];

    if (gesture.state == UIGestureRecognizerStateBegan) {

        dragViewSize = gesture.view.frame.size;

    } else if (gesture.state == UIGestureRecognizerStateChanged) {

        CGRect _rect = gesture.view.frame;
        _rect.size.width=dragViewSize.width+translation.x;
        gesture.view.frame = _rect;

    }
    else if (gesture.state == UIGestureRecognizerStateEnded) {

    }
}

它工作正常......希望它会帮助你......

于 2013-06-21T04:38:04.387 回答
0

正如我在对 Mundi 的评论中所写的那样,该方法- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight已被弃用,因此在任何新开发中使用它都是不明智的。

因此,您应该按照@msgambel 的建议继续使用- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsetsiOS 5+ 兼容性,甚至更好地- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode兼容 iOS 6+。

永远不要使用已弃用的方法:)

于 2013-06-20T06:09:03.773 回答