我尝试使用这些功能调整图像视图的大小:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* mTouch = [touches anyObject];
if (mTouch.view == [self Logo_01]) {
CGPoint cp = [mTouch locationInView:[self view]];
[[mTouch view]setCenter:CGPointMake(cp.x-xd, cp.y-yd)];
NSLog(@"questo è x");
NSLog(@"lalal %f", cp.y);
if (cp.y > 390) {
[_Logo_01 setHidden:YES];
}
if (cp.y < 130) {
[_Logo_01 setHidden:YES];
}
if (cp.x > 290) {
[_Logo_01 setHidden:YES];
}
if (cp.x < 40) {
[_Logo_01 setHidden:YES];
}
}
当我在应用程序上调整我的徽标大小时,图像正确调整大小,但我的中心点错误
对不起,我很冤枉发帖 这是我用来缩放图像的代码:
- (IBAction)ScaleImage1:(UIPinchGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
CGFloat height = _Logo_01.frame.size.height;
NSLog(@"SIZE ===== %f", height);
if (height > 600) {
[_Logo_01 setHidden:YES];
}
}
答案 0 :(得分:0)
Logo_01
是UI元素的可怕名称。这对资产名称有好处。重构并将其重命名为例如startingLogoImageView
。 ==
和isEqual
进行比较可以采用不同的方式。一个比较指针,另一个对象。请仔细阅读,因为如果这是非故意的,这是一个严重的错误。 xd
行中的setCenter:
是什么?它来自哪里?cp
touchPoint
,因为事实上它更明显。这是Apple推荐并由开发者使用的方式。所以代码:
// extern NSUInteger const kViewOffsetY; // uncomment this line if you want it accessible via other classes, delete otherwise
NSUInteger const kViewOffsetY = 1;
NSUInteger const kViewOffsetX = 1;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch.view isEqual:startingLogoImageView]) {
UIView *touchedView = touch.view;
CGPoint touchPoint = [touch locationInView:self.view];
[touchedView setCenter:CGPointMake(CGRectGetMidX(touchedView.frame) - kViewOffsetX, CGRectGetMidY(touchedView.frame) - kViewOffsetY)];
// where are these values from? you probably should calculate it more dynamically
BOOL shouldHideView = touchPoint.y > 390 || touchPoint.y < 130 || touchPoint.x > 290 || touchPoint.x < 40;
if (shouldHideView) {
[touchedView setHidden:YES];
}
}
}
为什么它工作错误?因为在setCenter:
方法中您使用的是左上角(x
和y
视图值位于左上角而不在中心位置),而您应该使用中心点。