我有这个图像是 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];
}