0

我想做什么:

我想制作一个根据它的当前状态放大或缩小的按钮,该状态由它的大小决定。

当按钮大并且我按下它时,它会变小。当按钮很小并且我按下它时,它会变大。

该按钮必须包含 QR 码的图像,并且随后可由 QR 阅读器扫描。

我的问题:

是不是图像大时模糊。如果我将图像放在 ImageView 而不是按钮中,情况并非如此。

效果最好在这里描述:http: //imgur.com/a/h3rJg#0

编码:

按钮/图像声明

UIImage* image = [QREncoder encode:user];
// declare image - QREncoder can be found on github
UIImage *strechedBackground = [image stretchableImageWithLeftCapWidth:220 topCapHeight:220];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
CGFloat qrSize = self.view.bounds.size.width - kPadding * 2;
CGRect largeFrame = CGRectMake(kPadding, (self.view.bounds.size.height - qrSize) / 2,
                          qrSize, qrSize);
imageView.frame = largeFrame;
[imageView layer].magnificationFilter = kCAFilterNearest;

//[self.view addSubview:imageView];

// add qr button to the view
self.qrButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.qrButton.frame = largeFrame;
[self.qrButton setBackgroundImage:image forState:UIControlStateNormal];
[self.qrButton addTarget:self
           action:@selector(aMethod)
 forControlEvents:UIControlEventTouchDown];
self.qrButton.contentMode = UIViewContentModeCenter;    


[self.view addSubview:self.qrButton];

按钮/图像缩放功能

- (void)aMethod{
// some variables concerned with button sizes and dimensions
CGFloat qrSize = self.view.bounds.size.width - kPadding * 2;
CGRect largeFrame = CGRectMake(kPadding, (self.view.bounds.size.height - qrSize) / 2,
                               qrSize, qrSize);

CGFloat smallButtonWidth = 33.0;
CGFloat smallButtonHeight = 33.0;

CGFloat largeButtonWidth = 264.0;
CGFloat largeButtonHeight = 264.0;

CGRect smallFrame = CGRectMake(self.view.frame.size.width - smallButtonWidth - kPadding/2, self.view.frame.size.height - smallButtonHeight - kPadding/2, smallButtonWidth, smallButtonHeight);
// a boolean ivar indicates the size of the button
if (!self.qrButtonIsBig){

    NSLog(@"Button was Big");


    // animate the view...
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         //self.qrButton.transform = CGAffineTransformMakeScale(3, 3);
                         // resting positon after animation
                         self.qrButton.frame = smallFrame;
                     }
                     completion:nil];

    NSLog(@"Button is now Small");


}else{

    NSLog(@"Button was Small");

    // animate the view...
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.qrButton.transform = CGAffineTransformIdentity;
                         // resting positon after animation, bottom left with 20 px margin
                         self.qrButton.frame = largeFrame;
                     }
                     completion:nil];

    NSLog(@"Button is now Big");

}
self.qrButtonIsBig = !self.qrButtonIsBig;

}

4

2 回答 2

0

由于 ImageView 很清晰,而 ButtonImage 很模糊。使用顶部的不可见按钮缩放图像视图作为一种实际工作是有意义的,而不是试图让图像在按钮中清晰。

于 2014-01-11T01:47:27.943 回答
0

如果您需要在调整大小时使视图更清晰,那么您需要将 CALayer 类设为 CATiledLayer。您可以对其进行子类化,以定义如何填充瓷砖。 如果您想走这条路, 请检查此问题 如果您不想使用 CATiledLayer,请检查此问题

于 2013-12-29T20:08:17.567 回答