捏合手势时减小屏幕上 UIButton 的大小
Decrease size of UIButtons on Screen upon Pinch Gesture
屏幕上有几个 UIButtons
。如果出现捏合手势,我想减小按钮的大小(如缩小效果)并添加更多按钮。我将如何实施它?
我是直接在 Whosebug 上输入的,所以可能会有错别字。
这些功能留作 OP 的练习:
- 通过连续捏合逐渐缩小。您必须有另一个私有 属性 来保存当前比例值。
- 在缩小效果后添加新按钮。
代码:
@interface MyViewController : UIViewController
@property(nonatomic, strong) NSMutableArray* buttons;
- (void)pinched:(UIPinchGestureRecognizer*)gesture;
@end
@implementation MyViewController
- (void)loadView {
[super loadView];
self.buttons = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(
0.0f,
(44.0f + 10.0f) * (CGFloat)i,
100.0f,
44.0f
)];
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[self.buttons addObject:button];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinched:)];
[self.view addGestureRecognizer:pinch];
}
- (void)pinched:(UIPinchGestureRecognizer*)gesture {
if (gesture.scale > 1.0) {
return;
}
for (UIButton* button in self.buttons) {
[UIView
animateWithDuration:1.0
animations:^void() {
button.transform = CGAffineTransformMakeScale(0.5, 0.5);
}
];
}
}
@end
屏幕上有几个 UIButtons
。如果出现捏合手势,我想减小按钮的大小(如缩小效果)并添加更多按钮。我将如何实施它?
我是直接在 Whosebug 上输入的,所以可能会有错别字。
这些功能留作 OP 的练习:
- 通过连续捏合逐渐缩小。您必须有另一个私有 属性 来保存当前比例值。
- 在缩小效果后添加新按钮。
代码:
@interface MyViewController : UIViewController
@property(nonatomic, strong) NSMutableArray* buttons;
- (void)pinched:(UIPinchGestureRecognizer*)gesture;
@end
@implementation MyViewController
- (void)loadView {
[super loadView];
self.buttons = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(
0.0f,
(44.0f + 10.0f) * (CGFloat)i,
100.0f,
44.0f
)];
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[self.buttons addObject:button];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinched:)];
[self.view addGestureRecognizer:pinch];
}
- (void)pinched:(UIPinchGestureRecognizer*)gesture {
if (gesture.scale > 1.0) {
return;
}
for (UIButton* button in self.buttons) {
[UIView
animateWithDuration:1.0
animations:^void() {
button.transform = CGAffineTransformMakeScale(0.5, 0.5);
}
];
}
}
@end