我有这张图片是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];
}
答案 0 :(得分:1)
好主意!是的,这是可能的。您可以有三个图像,左,右和中心,中间一个只有一个像素宽。
您可以在touchesMoved
中调整图像。在那里,您可以根据触摸位置计算必要的拉伸宽度。
使用
等方法可以轻松拉伸 [UIImage stretchableImageWithLeftCapWidth:topCapHeight]
(UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
resizingMode:(UIImageResizingMode)resizingMode
答案 1 :(得分:0)
正如我在对Mundi的评论中所写,方法- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
已被弃用,因此在任何新开发中使用它都是不明智的。
因此,根据@msgambel建议,您应该转移到- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
以获得iOS 5+兼容性,或者更好- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
以获得iOS 6+兼容性。
永远不要使用弃用的方法:)
答案 2 :(得分: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) {
}
}
它的工作正常......希望它会帮助你...
答案 3 :(得分: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 :)
}