我可以使UIPinchGestureRecognizer
處理程序與縮放對象一起工作,但我不想縮放,我想更改大小。例如,我有一個UITextView
,我附加了一個UIPinchGestureRecognizer
手勢,如果用戶捏我想改變textview的寬度以匹配捏。我不想縮放它以便UITextView
變大(縮放)。如何使用縮放(UIPinchGestureRecognizer)來改變UITextView的寬度?
回答
我想你想要做的就是乘以你的TextView的框架與手勢識別的規模寬度什麼:
CGFloat scale = gestureRecognizer.scale;
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(scale*newFrame.size.width, newFrame.size.height);
textView.frame = newFrame;
,或者不是此你是什麼意思?
這似乎並不奏效。 uitextview增長得非常快,並且延遲了。 規模可以從0-20 + – 2010-04-22 14:13:10
奇怪的...我不會有這樣的預期。我從來沒有真正使用過手勢識別器... – 2010-04-22 15:16:06
爲什麼不刪除這個答案? – 2010-12-17 21:25:58
我正在做同樣的事情。 我會更新這篇文章,如果我發現如何做到這一點。
試試這個,它爲我工作(爲的UIView):
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
static CGRect initialBounds;
UIView *_view = sender.view;
if (sender.state == UIGestureRecognizerStateBegan)
{
initialBounds = _view.bounds;
}
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
_view.bounds = CGRectApplyAffineTransform(initialBounds, zt);
return;
}
真正幫助我的是_view.bounds = CGRectApplyAffineTransform(initialBounds,zt);想知道爲什麼我的界限大小沒有變化..非常感謝! – Oritm 2012-06-05 08:43:41
這段代碼中有很多錯誤。運行此代碼時,總是碰到 – user2083364 2013-08-02 08:34:21
... – HoangNA 2017-01-20 08:58:26
我有四個程序與文本字段的捏處理。手勢識別器是核心例程。它會查看所選文本字段是否將被捏在屏幕上,我不希望那樣。如果他們不是,那麼我會告訴它用自己的姿勢來縮小自己的尺度。如果有多個選擇,我會發送一個通知給那些不會夾斷屏幕的人捏自己。
//--------------------------------------------------------------------------------------------------------
// pinchElement
// Description: Called to di the element scale, in our case, we are adjusting the length.
//
//--------------------------------------------------------------------------------------------------------
- (void)pinchElement:(CGFloat)scale {
//Grab how big we are now
CGRect textFieldBounds = textField.bounds;
//Multiple the Scale of the Pinch by the Width to get our new width.
CGFloat newWidth = textFieldBounds.size.width * scale;
CGFloat widthChange = newWidth - textFieldBounds.size.width;
CGRect newBounds = CGRectMake(0, 0, newWidth, textFieldBounds.size.height);
[textField setBounds: newBounds];
[textField setCenter: CGPointMake(textField.center.x + widthChange/2, textField.center.y)] ;
[self contentSizeChanged];
}
//--------------------------------------------------------------------------------------------------------
// pinchOffScreen
// Description: Called to see if the Pinch Gesture will cause element to go off screen Gesture
//
//--------------------------------------------------------------------------------------------------------
- (BOOL)pinchOffScreen:(CGFloat)scale {
//Grab how big we are now
CGRect textFieldBounds = textField.bounds;
//Multiple the Scale of the Pinch by the Width to get our new width.
CGFloat newWidth = textFieldBounds.size.width * scale;
//Figure out our Change in Width so we can calculate our new Zen Center
CGRect newElementBounds = CGRectMake(0, 0, newWidth+ kElementFrameOffset*2 + kElementContentFrameOffset*2, textFieldBounds.size.height + kElementFrameOffset*2 + kElementContentFrameOffset*2);
//We want to be sure that we dont size beyond our bounds, find our Parent Origin.
CGRect elementBoundsInSuperView = [self convertRect:newElementBounds toView:[self superview]];
CGFloat xPosition = CGRectGetMidX(elementBoundsInSuperView);
CGFloat yPosition = CGRectGetMidY(elementBoundsInSuperView);
BOOL offScreen = [self calcOffEditorFromXposition:xPosition yPosition:yPosition fromBoundsInSuperView:elementBoundsInSuperView];
return offScreen;
}
//--------------------------------------------------------------------------------------------------------
// handlePinchGesture
// Description: Called when we get a Pinch Gesture
// We want to override the default scaling and set the width.
//
//--------------------------------------------------------------------------------------------------------
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
if (IoUIDebug & IoUIDebugSelectorNames) {
NSLog(@"%@ - %@", INTERFACENAME, NSStringFromSelector(_cmd));
}
// UIView *element = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
//We are resizing, Select ourself
[self selectSelf];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
NSSet *selectedElements = [[(IoScreenEditorViewController *)UIAppDelegate.ioMainViewController.currentViewController editorContentViewController] selectedElements];
BOOL aSelectedElementOffscreen = FALSE;
for (IoUIScreenElement* element in selectedElements) {
if ([element pinchOffScreen:[gestureRecognizer scale]]) {
aSelectedElementOffscreen = TRUE;
break;
}
}
if (!aSelectedElementOffscreen) {
[self pinchElement:[gestureRecognizer scale]];
// Let others know they are moving if they are selected
// Setup our data for the Notification
NSMutableDictionary *theUserInfo = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
[theUserInfo setObject:self forKey:@"ElementWithGesture"];
NSNumber * scaleAsNumber = [[NSNumber alloc] initWithFloat:[gestureRecognizer scale]];
[theUserInfo setValue:scaleAsNumber forKey:@"GestureScale"];
[theUserInfo setObject:gestureRecognizer forKey:@"TheGestureRecognizer"];
[scaleAsNumber release];
// Post the Group Rotation Notification.
[[NSNotificationCenter defaultCenter] postNotificationName:kNCSEGroupPinchGesture
object:nil
userInfo:theUserInfo];
}
[gestureRecognizer setScale:1];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
}
}
//--------------------------------------------------------------------------------------------------------
// groupHandlePinchGesture:
// Description: For a groupPinch Notification. Move it! within bounds of course
//
//--------------------------------------------------------------------------------------------------------
- (void) groupHandlePinchGesture:(NSNotification*)notification{
if (IoUIDebug & IoUIDebugSelectorNames) {
NSLog(@"%@ - %@", INTERFACENAME, NSStringFromSelector(_cmd));
}
IoUIScreenElement *element = (IoUIScreenElement *) [[notification userInfo] objectForKey:@"ElementWithGesture"];
//UIRotationGestureRecognizer *gestureRecognizer = (UIRotationGestureRecognizer *) [[notification userInfo] objectForKey:@"TheGestureRecognizer"];
NSNumber *scaleAsNumber = [[notification userInfo] valueForKey:@"GestureScale"];
CGFloat scale = [scaleAsNumber floatValue];
if (IOFNOTEQUAL(self, element) & [self isSelected]){
[self pinchElement: scale];
}
}
你必須迅速使用:
func pinchgsterAction(gesture:UIPinchGestureRecognizer){
if (gesture.state == UIGestureRecognizerState.Changed) {
let scale:CGFloat = gesture.scale
gesture.view.transform = CGAffineTransformScale(gesture.view.transform, scale, scale)
}
}
- 1. 如何使用縮放(UIPinchGestureRecognizer)來改變UIView的寬度
- 2. 使用UIPinchGestureRecognizer來縮放圖層
- 3. UIPinchGestureRecognizer(縮小/放大)
- 4. UIPinchGestureRecognizer縮放AVCaptureConnection;不縮小
- 5. 如何使用寬度和比例來縮放UIImage
- 6. 使用UIPinchGestureRecognizer縮放整個屏幕
- 7. 如何用CSS縮放SVG寬度?
- 8. 如何使用CSS縮放元素的高度和寬度?
- 9. UIPinchGestureRecognizer縮放所有視圖
- 10. Sprite-Kit縮放問題UIPinchGestureRecognizer
- 11. 如何使用JavaScript中的變量來改變DIV的寬度和高度?
- 12. ImageView縮放:使用maxWidth修復高度可變寬度
- 13. 縮放div寬度
- 14. 使用縮放動畫更改EditText寬度,而不縮放其中的字符?
- 15. 我應該如何使用UIPinchGestureRecognizer縮放此地圖圖片?
- 16. php imgcopyresampled - 改變縮略圖的寬度
- 17. 使用SVG時高度和寬度不縮放使用SVG時高度和寬度不縮放
- 18. WinRTXamlCalendar:如何改變寬度?
- 19. 如何改變TD寬度?
- 20. 在最大寬度懸停時變換縮放「縮放」?
- 21. UITextview縮放?
- 22. 如何使用JavaScript獲取縮放SVG元素的寬度?
- 23. 使用最小寬度和最大寬度縮放CSS Tile Grid
- 24. 如何用Three.js改變CubeGeometry的寬度?
- 25. 更改UITextView中指示器的寬度
- 26. 如何使用UIPinchGestureRecognizer
- 27. 網站寬度縮放
- 28. 縮放文本和寬度
- 29. 如何計算UITextview的多行寬度?
- 30. 如何調整UITextView的寬度根據?
你不應該攻擊手無寸鐵UIPinchGestureRecognizers:P – 2010-04-22 13:58:21
@KennyTM哈哈!好的... – 2010-04-22 21:18:29
@Jumama爲什麼不呢?它可能先捏他! – 2012-08-31 16:47:46