美文网首页
iOS 遇到的问题和解决方法(持续更新)

iOS 遇到的问题和解决方法(持续更新)

作者: 我唔知啊 | 来源:发表于2017-03-04 13:06 被阅读62次
    1、 指定CollectionView的Item滑动到顶部时,Section会盖住Item部分。
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.row] 
                                atScrollPosition:UICollectionViewScrollPositionTop 
                                        animated:YES];
    

    效果如图中红色框内:


    CollectionView.png

    暂时解决办法:换个显示Item的位置,即把UICollectionViewScrollPositionTop改成UICollectionViewScrollPositionCenteredVertically(中间)| UICollectionViewScrollPositionBottom(底部)

    2、拖动View到屏幕边角时,layer贴紧边角。

    知识点:Layer.position和Layer.anchorPoint


    贴紧左上角.gif

    代码:

    static CGFloat MappingViewWidth = 100;
    static CGFloat MappingViewHeight = 100;
    
    - (void)testMappingMode {
        self.mappingView = [UIView new];
        self.mappingView.layer.contents = (id)[UIImage imageNamed:@"mapping_r1"].CGImage;
        [self.view addSubview:self.mappingView];
        // 这里用的是PureLayout,可换成其他
        self.mappingViewXConstraint = [self.mappingView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view withOffset:100.];
        self.mappingViewYConstraint = [self.mappingView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view withOffset:100.];
        [self.mappingView autoSetDimensionsToSize:CGSizeMake(MappingViewWidth, MappingViewHeight)];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveMappingView:)];
        [self.mappingView addGestureRecognizer:pan];
        
        self.combineLayer = [CALayer layer];
        self.combineLayer.frame = CGRectMake(0, 0, 200, 200);
        self.combineLayer.contents = (id)[UIImage imageNamed:@"mapping_map"].CGImage;
        // self.combineLayer添加到self.view.layer,相对位置基于self.view
        [self.view.layer insertSublayer:self.combineLayer below:self.mappingView.layer];
        self.combineLayer.position = CGPointMake(150, 150);
        self.combineLayer.anchorPoint = CGPointMake(0.5, 0.5);
    }
    
    - (void)moveMappingView:(UIPanGestureRecognizer *)gesture {
        CGPoint location = [gesture locationInView:self.view];
    
        // 移动View
        self.mappingViewXConstraint.constant = location.x-MappingViewWidth/2.;
        self.mappingViewYConstraint.constant = location.y-MappingViewHeight/2.;
    
        // 移动layer
        if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) {
            // 如果也要贴紧右上角,修改条件和position即可
            if (location.x <= 100 && location.y > 100) {
                self.combineLayer.position = CGPointMake(0, location.y-MappingViewHeight/2.-50);
                self.combineLayer.anchorPoint = CGPointMake(0, 0);
            }else if (location.y <= 100 && location.x > 100) {
                self.combineLayer.position = CGPointMake(location.x-MappingViewWidth/2.-50, 0);
                self.combineLayer.anchorPoint = CGPointMake(0, 0);
            }else if (location.y <= 100 && location.x <= 100) {
                self.combineLayer.position = CGPointMake(0, 0);
                self.combineLayer.anchorPoint = CGPointMake(0, 0);
            }else {
                self.combineLayer.position = CGPointMake(location.x, location.y);
                self.combineLayer.anchorPoint = CGPointMake(0.5, 0.5);
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 遇到的问题和解决方法(持续更新)

          本文链接:https://www.haomeiwen.com/subject/iikdgttx.html