Skip to content

Instantly share code, notes, and snippets.

@gadmyth
Created July 10, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadmyth/d7ea0cb86ce24d216b765107e9391156 to your computer and use it in GitHub Desktop.
Save gadmyth/d7ea0cb86ce24d216b765107e9391156 to your computer and use it in GitHub Desktop.
AnchorPoint test
//
// ViewController.m
// AnchorPointTest
//
// Created by gadmyth on 7/10/17.
// Copyright © 2017 39sports. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) UITextField *t1;
@property (strong, nonatomic) UITextField *t2;
@property (strong, nonatomic) CALayer *mLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_t1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, 100, 40)];
_t1.borderStyle = UITextBorderStyleRoundedRect;
_t1.placeholder = @"请输入0~1内值";
_t2 = [[UITextField alloc] initWithFrame:CGRectMake(120, 70, 100, 40)];
_t2.borderStyle = UITextBorderStyleRoundedRect;
_t2.placeholder = @"请输入0~1内值";
[self.view addSubview:_t1];
[self.view addSubview:_t2];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 120, 60, 40)];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button addTarget:self action:@selector(anchorPointChanged:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
_mLayer = [CALayer layer];
_mLayer.backgroundColor = [[UIColor alloc] initWithRed:0.7 green:0.1 blue:0.3 alpha:0.5].CGColor;
_mLayer.frame = CGRectMake(20, 200, 100, 100);
[self.view.layer addSublayer:_mLayer];
}
- (void)anchorPointChanged:(UIControl *)sender {
CGFloat x = [_t1.text floatValue];
CGFloat y = [_t2.text floatValue];
_mLayer.anchorPoint = CGPointMake(x, y);
CGFloat anchorX = _mLayer.bounds.origin.x + x * _mLayer.bounds.size.width;
CGFloat anchorY = _mLayer.bounds.origin.y + y * _mLayer.bounds.size.height;
CGPoint anchorP = CGPointMake(anchorX, anchorY);
CGPoint anchorInSuper = [_mLayer convertPoint:anchorP toLayer:self.view.layer];
NSLog(@"assert the anchorPoint and Position: %@", @(CGPointEqualToPoint(_mLayer.position, anchorInSuper)));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment