UIViewController touch events

本文介绍如何在Objective-C中创建一个自定义视图,并实现触摸事件处理功能。

需要创建一个UIView, 把touch event写在此UIView里, 然后在controller里创建此UIView

@interface YourCustomView : UIView

@implementation YourCustomView

// Override this function to get the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"TOUCH!");
}

 

@interface YourViewController : UIViewController
{
YourCustomView* view
}




转载于:https://www.cnblogs.com/pengyingh/articles/2381978.html

extension OnlineDeviceListViewController { //MARK: D - UICollectionViewDelegate override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { super.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath) // ========== 1. 统一处理 BaseDeviceUIItem 类型 ========== if let item = cell as? BaseDeviceUIItem { item.settingButton.removeTarget(nil, action: nil, for: .allEvents) item.settingButton.addTarget(self, action: #selector(handleChannelShowMoreMenu(_:)), for: .touchUpInside) } // ========== 2. 底部圆角处理(仅最后一项)========== let numberOfItems = collectionView.numberOfItems(inSection: indexPath.section) cell.layer.mask = nil if indexPath.item == numberOfItems - 1 { let cornerLayer = CAShapeLayer() let radius = kBorderRadius let path = UIBezierPath( roundedRect: cell.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: radius, height: radius) ) cornerLayer.path = path.cgPath cornerLayer.frame = cell.bounds cornerLayer.masksToBounds = true cell.layer.mask = cornerLayer } // ========== 3. 特殊处理 OnlineDeviceCardNormalHeader ========== guard let header = cell as? OnlineDeviceCardNormalHeader else { return } // --- helpButton --- header.ipcView.helpButton?.removeTarget(nil, action: nil, for: .allEvents) header.ipcView.helpButton?.tag = indexPath.item header.ipcView.helpButton?.addTarget(self, action: #selector(showHelp(_:)), for: .touchUpInside) // --- collecAction --- header.collecAction = { [weak self] (collect, device, channel, offset) in guard let strongSelf = self else { return } strongSelf.contentOffsetForNVRChannelCollect = offset strongSelf.collectButtonClicked(isCollect: collect, device: device, channel: channel) } // --- channelSelected --- header.channelSelected = { [weak self] (device, channel) in guard let strongSelf = self else { return } strongSelf.goToPreview(channel: channel, nvr: device) } // --- scrollCallback --- header.scrollCallback = { [weak self] (visibles) in guard let strongSelf = self else { return } strongSelf.preconnectToRelay() strongSelf.snapShotToVMS(isHorizontalScroll: true) if let deviceId = strongSelf.deviceList[safe: indexPath.item]?.identifier { var list = [TPSSDeviceId]() for item in visibles { let device = TPSSDeviceId() device.deviceId = deviceId device.channelID = item.row list.append(device) } if list.count > 0 && (strongSelf.downloadPreviewSnapshotHorizontalScrollRequestID ?? 0) > 0 { strongSelf.downloadSnapShotHorizontalVisiblePreviewHashMap[ strongSelf.downloadPreviewSnapshotHorizontalScrollRequestID! ] = list } } } // --- 恢复 contentOffset --- let device = deviceList[indexPath.item] if let offset = offsetDictionary[device.identifier], offset.x + header.channelListVerticalCollectionView.frame.size.width - 2 * deviceListHorizontalMargin < header.channelListVerticalCollectionView.contentSize.width { header.channelListVerticalCollectionView.contentOffset = offset } else { header.channelListVerticalCollectionView.contentOffset = CGPoint(x: 0, y: 0) } // 如果是收藏操作触发 reload,恢复之前的 offset if collectDevice?.identifier == device.identifier && collectDevice?.deviceType == .NVR { header.channelListVerticalCollectionView.contentOffset = contentOffsetForNVRChannelCollect ?? CGPoint.zero } // --- 圆角处理(折叠状态)--- if isFolded(at: indexPath.item) { header.layer.masksToBounds = true header.layer.cornerRadius = kBorderRadius if #available(iOS 11.0, *) { header.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner] } } else { header.layer.cornerRadius = 0 if #available(iOS 11.0, *) { header.layer.cornerRadius = kBorderRadius header.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] } else { let corner = UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.topRight.rawValue) setCornerForViewWithCorner(view: header, corner: corner, radius: kBorderRadius) } } } // MARK: - 移除 willDisplaySupplementaryView 或简化为仅处理非设备header override func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) { super.collectionView(collectionView, willDisplaySupplementaryView: view, forElementKind: elementKind, at: indexPath) view.indexPath = indexPath // ⚠️ 只保留 VMS 分页加载逻辑(如果还有站点 header 才需要) if appContext.isVmsLogin && !showLocalDeviceOnly { let row = indexPath.section if (row >= vmsDevices.count - 5 && !(showCollectPage ? appContext.devicesIsLastPage(inVMSFavoriteSiteId: currentSubSiteInfo?.siteId ?? 0, devStatus: Int32(deviceOnlineStatus.rawValue)) : appContext.devicesIsLastPage(inVMSSiteId: currentSubSiteInfo?.siteId ?? 0))) { getNextPage() } } // 如果你有其他类型的 header(如 OnlineDeviceListHeader),可以单独处理 if let item = view as? BaseDeviceUIItem { item.settingButton.removeTarget(nil, action: nil, for: .allEvents) item.settingButton.tag = indexPath.section item.settingButton.addTarget(self, action: #selector(handleDeviceShowMoreMenu(_:)), for: .touchUpInside) } if let item = view as? BaseDeviceSDErrorItem { item.sdErrorButton.removeTarget(nil, action: nil, for: .allEvents) item.sdErrorButton.tag = indexPath.section item.sdErrorButton.addTarget(self, action: #selector(sdCardErrorClicked(_:)), for: .touchUpInside) } if let item = view as? BaseDeviceBindItem { item.bindButton.removeTarget(nil, action: nil, for: .allEvents) item.bindButton.tag = indexPath.section item.bindButton.addTarget(self, action: #selector(handleLocalDeviceBind(_:)), for: .touchUpInside) } // 处理分组 header 的圆角(比如站点 header) if let item = view as? OnlineDeviceListHeader { if isFolded(at: indexPath.section) { item.layer.masksToBounds = true item.layer.cornerRadius = kBorderRadius } else { item.layer.cornerRadius = 0 let corner = UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.topRight.rawValue) setCornerForViewWithCorner(view: item, corner: corner, radius: kBorderRadius) } } } // MARK: - didEndDisplayingCell 替代 didEndDisplayingSupplementaryView func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { // 记录 contentOffset 当 cell 滚出屏幕 guard collectionView.isDragging || collectionView.isDecelerating, let header = cell as? OnlineDeviceCardNormalHeader, indexPath.item < deviceList.count else { return } let offset = header.channelListVerticalCollectionView.contentOffset let device = deviceList[indexPath.item] offsetDictionary[device.identifier] = offset } } // 原来的 supplementaryView 结束显示方法可删除或留空 @objc(collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:) func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) { // 已由 cell 版本接管,此处可忽略 } } 帮我完整修改这部分代码,现在报了很多错找不到Value of type 'OnlineDeviceListViewController' has no member 'deviceList'
最新发布
10-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值