我想完全像这样输入链接描述
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); CGFloat lastScaleFactor=1; CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (factor > 1) { //zooming in _textView.transform=CGAffineTransformMakeScale(lastScaleFactor + (factor-1),lastScaleFactor + (factor -1)); } else { _textView.transform=CGAffineTransformMakeScale(lastScaleFactor *factor, lastScaleFactor *factor +1); } if (sender.state==UIGestureRecognizerStateEnded) { if (factor >1) { lastScaleFactor += (factor-1); } else lastScaleFactor *= factor; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @endi want to do exactly like that enter link description here
最满意答案
在链接的视频上,不是正在改变的视图变换,而是字体大小(注意文本是如何重排的)。 只需将计算出的比例应用于textView的字体大小就可以了。
其次,你的lastScaleFactor应该可以定义为static ,否则它没有意义......尝试下面的行(没有测试甚至编译,但应该直接复制)。 下面的代码段假定您在textView中有纯文本。 如果您已归因于文本,则必须更改将新字体分配给文本视图的位置,而是循环遍历所有attributedText段并相应地修改每个段大小; 你也必须保留每个文本段的最后固定字体大小的临时备忘录,如下面对currentFontSize属性中的整个文本所做的那样
@interface ViewController () @property CGFloat currentFontSize; @end...
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.currentFontSize = _textView.font.pointSize } - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); const CGFloat scaleCoeff = 0.5; //play with this one CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (sender.state==UIGestureRecognizerStateEnded) { self.currentFontSize = _textView.font.pointSize; } else { _textView.font = [_textView.font fontWithSize:self.currentFontSize * factor * scaleCoeff]; } }On the linked video it is not the view transform that is changing, it is the font size (note how the text is reflowed). Simply apply your calculated scale to the textView's font size and that's it.
Secondly, your lastScaleFactor should probably be defined static, otherwise it doesn't make sense... Try something along the below lines (was not tested or even compiled, however should be straight forward to copy). The snippet below assumes you have plain text in the textView. In case you have attributed text, then you would have to change the place where a new font is assigned to the text view, and instead loop through all attributedText segments and modify each of the segments size accordingly; also you would have to keep temporary memo of the last fixed font size per each of the text segments, as it is done below for the whole text in the currentFontSize property
@interface ViewController () @property CGFloat currentFontSize; @end...
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.currentFontSize = _textView.font.pointSize } - (IBAction)pinchGesture:(UIGestureRecognizer *)sender{ NSLog(@"pinched"); const CGFloat scaleCoeff = 0.5; //play with this one CGFloat factor =[(UIPinchGestureRecognizer *)sender scale]; if (sender.state==UIGestureRecognizerStateEnded) { self.currentFontSize = _textView.font.pointSize; } else { _textView.font = [_textView.font fontWithSize:self.currentFontSize * factor * scaleCoeff]; } }更多推荐
发布评论