Iphone 拉伸按钮顶部的图像以按下视图控制器
我有一张图片,它是UIButton: 我想添加一些手势(平移手势)将其向右滑动,我希望图像向右拉伸,如下所示:Iphone 拉伸按钮顶部的图像以按下视图控制器,iphone,ios,objective-c,uibutton,uiimage,Iphone,Ios,Objective C,Uibutton,Uiimage,我有一张图片,它是UIButton: 我想添加一些手势(平移手势)将其向右滑动,我希望图像向右拉伸,如下所示: UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidDragged:)]; [self.buttonAbout addGestureRecognizer:panGesture]; self.buttonFr
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidDragged:)];
[self.buttonAbout addGestureRecognizer:panGesture];
self.buttonFrame = self.buttonAbout.frame;
它与iPhone的“滑动解锁”方法非常相似,但没有从左向右移动的相同图像
有可能吗
编辑:
我在按钮中添加了一个UIPangestureRecognitor,如下所示:
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];
}
好主意!是的,这是可能的。您可以有三个图像,左、右和中间,中间的一个只有一个像素宽 您可以在
触摸移动中调整图像。在那里,您可以根据触摸的位置计算必要的拉伸宽度
拉伸是相当容易的方法,如
[UIImage StretcableImageWithLeftCapWidth:topCapHeight]
正如我在给Mundi的评论中所写的那样,leftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
方法已被弃用,因此在任何新开发中使用它是不明智的
因此,您应该按照@msgambel的建议,继续使用iOS 5+兼容性的-(UIImage*)调整大小的图像和capInsets:(UIEdgeInsets)capInsets
,或者更好的-(UIImage*)调整大小的图像和capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
,以实现iOS 6+兼容性
永远不要使用不推荐的方法:)像这样做,
在.h
CGSize dragViewSize;
那么
它的工作很好。。。希望它能帮助你……最后,这就是我所做的,而且效果非常好。非常感谢大家
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 :)
}
CGPoint translation=[手势翻译视图:self.view];
UIButton*按钮=(UIButton*)手势.view;
if(translation.xktransationminimum)
{
self.aboutCenterImage.frame=CGRectMake(self.aboutCenterImage.frame.origin.x,
self.aboutCenterImage.frame.origin.y,
翻译.x,
自我。关于中心图像。框架。尺寸。高度);
手势.view.frame=CGRectMake(self.aboutCenterImage.frame.origin.x+self.aboutCenterImage.frame.size.width,
self.aboutCenterImage.frame.origin.y,
self.buttonAbout.frame.size.width,
自我。关于中心图像。框架。尺寸。高度);
}
else if(translation.x>ktransationmax)
{
//按ViewController或执行任何您想执行的操作:)
}
作为旁白,使用方法[[UIImage ImageName:@”“]ResizebleimageWithCapInsets:UIEdgeInsetsMake(上边缘、左边缘、下边缘、右边缘)]
对如何拉伸图像提供了更多的控制。@YossiTsafar:-(UIImage*)StretcableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
不推荐使用。所以用这个是不明智的。如果您需要iOS 5支持,请按照@msgambel的建议执行,或者使用iOS 6+的-(UIImage*)大小可调图像和capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
。@msgambel仍然不知道如何做到这一点。这行不通。按钮向右滑动,但图像重新混合相同。你能告诉我这是为什么吗?@YossiTsafar,看起来你在为中心点设置动画,但不是画面本身。如果在动画块中增加按钮图像的宽度,则应允许图像拉伸。再次对其进行更改,但没有任何更改。我编辑了我的代码,请检查一下。我不知道为什么图像没有延伸。伙计们,我下班了,但周日的第一件事是我会检查答案。非常感谢你们所有人!
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 :)
}